-2
I have these tables in my database:
create table movimentacao (
id bigint not null,
usuario_id bigint not null,
data_entrada datetime not null,
data_saida datetime,
periodo_permanencia decimal(10,2),
ocorrencia_id bigint,
data_id bigint,
primary key (id, usuario_id)
) engine=InnoDB;
create table banco_horas (
id bigint not null,
movimentacao_id bigint not null,
usuario_id bigint not null,
data_trabalhada datetime not null,
horas_trabalhadas decimal(10,2) not null,
saldo_horas decimal(10,2) not null,
primary key (id, movimentacao_id, usuario_id)
) engine=InnoDB;
Since data bank has 3 primary keys, two of them being foreign key, I’m trying to map as follows:
@Getter
@Setter
@Entity
@Table(name = "banco_horas")
public class BancoHoras {
@EmbeddedId
private BancoHorasId id;
@MapsId("usuarioId")
@ManyToOne(optional = false)
@JoinColumn(name = "usuario_id")
private Usuario usuario;
@MapsId("movimentacaoId")
@ManyToOne(optional = false)
@JoinColumn(name = "movimentacao_id")
private Movimentacao movimentacao;
private LocalDateTime dataTrabalhada;
private BigDecimal quantidadeHorasTrabalhadas;
private BigDecimal saldoHorasTrabalhadas;
@Getter
@Setter
@Embeddable
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class BancoHorasId implements Serializable {
private static final long serialVersionUID = 1L;
@EqualsAndHashCode.Include
private Long id;
@EqualsAndHashCode.Include
@Column(name = "usuario_id")
private Long usuarioId;
@EqualsAndHashCode.Include
@Column(name = "movimentacao_id")
private Long movimentacaoId;
}
}
working class:
@Getter
@Setter
@Entity
@Table(name = "movimentacao")
public class Movimentacao {
@EmbeddedId
private MovimentacaoId id;
@MapsId("usuarioId")
@ManyToOne(optional = false)
@JoinColumn(name = "usuario_id")
private Usuario usuario;
private LocalDateTime dataEntrada;
private LocalDateTime dataSaida;
private BigDecimal periodoPermanencia;
@ManyToOne(optional = false)
@JoinColumn(name = "ocorrencia_id")
private Ocorrencia ocorrencia;
@ManyToOne(optional = false)
@JoinColumn(name = "data_id")
private Calendario data;
@Getter
@Setter
@Embeddable
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class MovimentacaoId implements Serializable {
private static final long serialVersionUID = 1L;
@EqualsAndHashCode.Include
private Long id;
@EqualsAndHashCode.Include
@Column(name = "usuario_id")
private Long usuarioId;
}
}
The problem is that this generating this error when I start the application, and only in the mapping of the drive table, since, commenting the code snippet referring to the property 'movement', the application does not give errors
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.9.jar:5.3.9]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.9.jar:5.3.9]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.9.jar:5.3.9]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.4.jar:2.5.4]
at com.digitalinnovationone.ControledepontoeacessoApiApplication.main(ControledepontoeacessoApiApplication.java:10) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.5.4.jar:2.5.4]
Caused by: org.hibernate.AnnotationException: Implicit column reference in the @MapsId mapping fails, try to use explicit referenceColumnNames: com.digitalinnovationone.model.Movimentacao
at org.hibernate.cfg.CopyIdentifierComponentSecondPass.createSimpleProperty(CopyIdentifierComponentSecondPass.java:192) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.cfg.CopyIdentifierComponentSecondPass.doSecondPass(CopyIdentifierComponentSecondPass.java:101) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1693) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processCopyIdentifierSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1684) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1653) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:295) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.9.jar:5.3.9]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.9.jar:5.3.9]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.9.jar:5.3.9]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.9.jar:5.3.9]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.9.jar:5.3.9]
... 21 common frames omitted
someone knows why this generating this error?