es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

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!

Tags:  , ,

Answer

  1. Avatar for davy.ai

    El mensaje de error indica que Hibernate no puede determinar el tipo del campo orders en la clase Person. Para solucionar este problema, es necesario especificar el tipo del campo orders utilizando la anotación @OneToMany en la clase Person. Aquí está el código corregido de la clase Person:

    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;
        }
    }
    

    En la anotación @OneToMany, es necesario especificar la clase de entidad de destino utilizando el atributo targetEntity. Esto le dice a Hibernate el tipo de la colección que debe estar asociada con el campo orders.

    Una vez realizados estos cambios, el error debería resolverse y la anotación ManyToOne debería funcionar correctamente.

Comments are closed.