Editado (7/10/20 18.55): Ahora está a saltar el siguiente error con respecto a la relación Empleado-Usuario.
Spring Boot Console
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.crmiguez.inventario_aixina.entities.Empleado.usuariosSet[com.crmiguez.xxxxxxxxxx.entities.Usuario]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1331) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:868) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:793) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1684) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1652) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:286) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:900) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:931) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
... 105 common frames omitted
Usuario.class
public class Usuario implements Serializable {
@Id
@GeneratedValue
@Column(name = "idusuarios")
private Long idusuarios;
@JoinColumn(name = "empleado_usuario", referencedColumnName = "idempleados")
@ManyToOne
private Empleado empleado_usuario;
@Column(name = "nombre_usuario")
private String nombre_usuario;
@Column(name = "email_usuario")
private String email_usuario;
@Column(name = "password_usuario")
private String password_usuario;
@Column(name = "ultima_conexion")
private String ultima_conexion;
@Column(name = "fecha_alta_usuario")
private String fecha_alta_usuario;
@Column(name = "fecha_baja_usuario")
private String fecha_baja_usuario;
@Column(name="es_administrador")
@ColumnDefault(value = "0")
private byte es_administrador;
public Usuario() {
}
//Getters and setters
}
Tabla Usuario en MySQL
Al arrancar el Spring Boot, salta una excepción de mapeo OneToMany, entre Empleado y Articulo. Lo que pretendo es integrar tanto ManyToOne como OneToMany entre las dos.
Spring Boot Console
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.crmiguez.xxxxxxxxxxxxxx.entities.Empleado.articulos in com.crmiguez.xxxxxxxxxxxxxx.entities.Articulo.empleadosSet
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:842) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:793) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1684) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1652) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:286) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:900) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:931) ~[hibernate-core-5.4.2.Final.jar:5.4.2.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
Las clases las tengo de la siguiente forma.
Articulo.class (Se guardarán los diferentes artículos)
@Entity(name = "articulos")
@Table
public class Articulo implements Serializable {
@Id
@GeneratedValue
@Column(name = "idarticulos")
private Long idarticulos;
@Column(name = "nombre_articulo")
private String nombre_articulo;
@Column(name = "fecha_alta_articulo")
private String fecha_alta_articulo;
@Column(name = "fecha_baja_articulo")
private String fecha_baja_articulo;
@Column(name = "numero_pegatina")
private String numero_pegatina;
@JoinColumn(name = "tipo_inmueble", referencedColumnName = "idtipo_inmueble", nullable = false)
@ManyToOne
private TipoInmueble tipo_inmueble;
@OneToMany(fetch = FetchType.LAZY, mappedBy="articulos")
@org.springframework.data.annotation.Transient
@JsonIgnore
private Set<Empleado> empleadosSet = new HashSet<>();
@OneToMany(mappedBy="articulos")
@org.springframework.data.annotation.Transient
@JsonIgnore
private Set<Inmueble> inmueblesSet = new HashSet<>();
@OneToMany(mappedBy="articulos")
@org.springframework.data.annotation.Transient
@JsonIgnore
private Set<Inventario> inventariosSet = new HashSet<>();
@OneToMany(mappedBy="articulos")
@org.springframework.data.annotation.Transient
@JsonIgnore
private Set<Movimiento> movimientosSet = new HashSet<>();
public Articulo() {
}
//Getters and setters
}
Empleado.class (Se guardarán la información de los empleados de la compañía)
@Entity(name = "empleados")
@Table
public class Empleado implements Serializable {
@Id
@GeneratedValue
@Column(name = "idempleados")
private Long idempleados;
@Column(name = "nombre_empleado")
private String nombre_empleado;
@Column(name = "apellidos_empleado")
private String apellidos_empleado;
@Column(name = "dni_empleado")
private String dni_empleado;
@JoinColumn(name = "departamento_empleado", referencedColumnName = "iddepartamentos")
@ManyToOne
private Departamento departamento;
@JoinColumn(name = "departamento_ubicacion", referencedColumnName = "iddepartamento")
@JoinColumn(name = "ubicacion_empleado", referencedColumnName = "idubicacion")
@ManyToOne
private UbicacionPlanta ubicacionPlanta;
@JoinColumn(name = "responsable_empleado", referencedColumnName = "idempleados")
@ManyToOne
private Empleado responsable_empleado;
@JoinColumn(name = "articulo_empleado", referencedColumnName = "idarticulos")
@ManyToOne
private Articulo articulo_empleado;
@JoinColumn(name = "inmueble_empleado", referencedColumnName = "idinmuebles")
@ManyToOne
private Inmueble inmueble_empleado;
@Column(name = "fecha_alta_empleado")
private String fecha_alta_empleado;
@Column(name = "fecha_baja_empleado")
private String fecha_baja_empleado;
@OneToMany (mappedBy = "empleados")
@org.springframework.data.annotation.Transient
@JsonIgnore
Set<Usuario> usuariosSet = new HashSet<>();
@OneToMany(mappedBy="empleados")
@org.springframework.data.annotation.Transient
@JsonIgnore
private Set<Inventario> inventariosSet = new HashSet<>();
@OneToMany(mappedBy = "empleados")
@org.springframework.data.annotation.Transient
@JsonIgnore
private Set<Movimiento> movimientosEmpSet = new HashSet<>();
@OneToMany(mappedBy = "responsable_emp")
@org.springframework.data.annotation.Transient
@JsonIgnore
private Set<Empleado> empleadosSet = new HashSet<>();
@OneToMany(mappedBy = "responsable_emp")
@org.springframework.data.annotation.Transient
@JsonIgnore
private Set<Movimiento> movimientosResponsableSet = new HashSet<>();
public Empleado() {
}
//Getters and setters
}
Las tablas en MySQL se muestran en las siguientes imágenes:
Alguna forma de mapear mejor estas entidades, por favor? Cómo tengo que indicarle a Hibernate que el mapeo tiene que ser una forma u otra?
Muchas gracias! :)
mappedBy="articulos"
amappedBy="articulo"
segun recuerdo es el nombre de la entidad no de la tabla la que debes colocar.