Issue
I am trying to use Sprint boot ManyToOne annotation on a field. My code looks like this for the Order
class
package com.example.demo_app.data.models;
import javax.persistence.*;
@Entity
public class Order extends Object {
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "person_id")
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
And the Person
class looks like this
package com.example.demo_app.data.models;
import javax.persistence.*;
import java.util.List;
@Entity
public class Person {
private Long id;
@OneToMany(targetEntity = Order.class)
private List<Order> orders;
public void setId(Long id) {
this.id = id;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
This is the error that I am getting
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.List, at table: person, for columns: [org.hibernate.mapping.Column(orders)]
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: person, for columns: [org.hibernate.mapping.Column(orders)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:512)
Any help is appreciated. Thanks!
Solution
You are getting an error because ORDER
is a SQL keyword.
Try changing table name of class Order
to something else
@Entity
@Table(name="ORDERS")
public class Order {
@Id
private Long id;
@ManyToOne
@JoinColumn(name = "person_id")
private Person person;
//getters and setters
}
Edit: This is Person
class, put annotations @Id
and @GeneratedValue
on id field, not on getter.
@Entity
@Table(name = "PERSON")
public class Person {
@Id
@GeneratedValue
private Long id;
@OneToMany(targetEntity = Order.class)
private List<Order> orders;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
}
Answered By - Grobocop12
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)