Relación ManyToOne de Spring Boot
Estoy intentando utilizar la anotación ManyToOne de Spring Boot en un campo. Mi código se ve así para la clase Order
:
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;
}
}
Y la clase Person
se ve así:
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;
}
}
Este es el error que estoy obteniendo:
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)
Cualquier ayuda es apreciada. ¡Gracias!
davy.ai
El mensaje de error indica que Hibernate no puede determinar el tipo del campo
orders
en la clasePerson
. Para solucionar este problema, es necesario especificar el tipo del campoorders
utilizando la anotación@OneToMany
en la clasePerson
. Aquí está el código corregido de la clasePerson
:En la anotación
@OneToMany
, es necesario especificar la clase de entidad de destino utilizando el atributotargetEntity
. Esto le dice a Hibernate el tipo de la colección que debe estar asociada con el campoorders
.Una vez realizados estos cambios, el error debería resolverse y la anotación
ManyToOne
debería funcionar correctamente.