Reputation: 618
I've got a database schema with following relations:
So I've created already all classes and I tried to insert some values to it. The problem I encountered is Reservation class, which contains Date fields (from, to).
package mapping;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name="Reservation")
public class Reservation {
private int id;
private Date from;
private Date to;
private Account purchaser;
private State state;
private House house;
public Reservation()
{
}
public Reservation(Date from, Date to)
{
this.from = from;
this.to = to;
}
@Id
@GeneratedValue
@Column(name="id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="from")
@Temporal(TemporalType.DATE)
public Date getFrom() {
return from;
}
public void setFrom(Date from) {
this.from = from;
}
@Column(name="to")
@Temporal(TemporalType.DATE)
public Date getTo() {
return to;
}
public void setTo(Date to) {
this.to = to;
}
@ManyToOne
@JoinColumn(name="purchaser_id")
public Account getPurchaser() {
return purchaser;
}
public void setPurchaser(Account purchaser) {
this.purchaser = purchaser;
}
@ManyToOne
@JoinColumn(name="state_id")
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
@ManyToOne
@JoinColumn(name="house_id")
public House getHouse() {
return house;
}
public void setHouse(House house) {
this.house = house;
}
}
and Main function:
package client;
import java.util.Calendar;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import mapping.Account;
import mapping.HibernateUtil;
import mapping.House;
import mapping.Reservation;
import mapping.State;
import mapping.Type;
public class Main {
public static void main(String[] args)
{
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
State state = new State("free");
session.save(state);
State state2 = new State("reserved");
session.save(state2);
Type type = new Type("flat house");
session.save(type);
Account temp = new Account("acc1","acc1","[email protected]","acc1","acc1");
temp.setType(type);
session.save(temp);
House house1 = new House("house1",120,"house1 Address",100000,"house");
house1.setState(state);
house1.setOwner(temp);
house1.setType(type);
session.save(house1);
Reservation res = new Reservation(new Date(),new Date());
res.setHouse(house1);
res.setPurchaser(temp);
res.setState(state2);
session.save(res);
session.getTransaction().commit();
session.close();
sf.close();
System.exit(0);
}
}
But when I compile it this error occurs:
WARN: SQL Error: 1064, SQLState: 42000 sty 08, 2017 5:25:34 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, house_id, purchaser_id, state_id, to) values ('2017-01-08', 9, 9, 18, '201' at line 1 Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement
Any advice will be appreciated.
p.s. I'm using MySql.
Upvotes: 0
Views: 875
Reputation: 691785
from
is a reserved SQL keyword. Choose another name for your column.
Upvotes: 3