I have a database with the following table structures…
The Customer table has two columns : id, name.
The Order table has two columns: id, customer_id
There is a one-to-one relation between these two tables
When I run my code, which is given below, I get the following exception:
Hibernate: insert into customer (name) values (?)
Hibernate: insert into order (customer_id) values (?)
05:02:46,374 WARN [main] JDBCExceptionReporter:233 - SQL Error: 0, SQLState: 42601
05:02:46,379 ERROR [main] JDBCExceptionReporter:234 - ERROR: syntax error at or near "order"
Here is my code. Could someone help to explain the cause of the problem…
@Entity
@Table(name = "customer")
public class Customer implements Serializable {
public Customer() {
}
private long id;
private String name;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", insertable = false, updatable = false, nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Entity
@Table(name = "order")
public class Order implements Serializable {
public Order() {
}
public Order(Customer customer) {
this.customer = customer;
}
private long id;
private Customer customer;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", insertable = false, updatable = false, nullable = false)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "customer_id")
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
// setCustomerId(customer.getId());
}
}
public static void main(String[] args) {
Customer cm = new Customer();
cm.setName("John");
Order ord = new Order(cm);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(ord);
transaction.commit();
}
catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
}
finally {
session.close();
}
}
orderis a SQL keyword. If you can’t change the database name, try using:But if you’re still in development, change table and column names to avoid using SQL keywords.