我遇到问题,因为我的表有自关系。我必须能够插入和更改列,但它总是给出此错误:列“idTipoDoctoEntrada”在实体“com.provider.fatoria.model.ConfigPergunta”的映射中重复(使用“@Column(insertable=false, updatable=false) '将多个属性映射到同一列时)。我不能这样做,因为我需要能够更改和插入。
ConfigPerguntaPk:
@Embeddable
public class ConfigPerguntaPk {
private Long idConfigPergunta;
@ManyToOne
@JoinColumn(name = "idTipoDoctoEntrada")
private TipoDoctoEntrada tipoDoctoEntrada;
ConfigPergunta:
@Entity
@Table(name = "tbConfigPergunta")
@DynamicUpdate
public class ConfigPergunta {
@EmbeddedId
private ConfigPerguntaPk configPerguntaPk;
private String txPergunta;
private String txAbaDestinoResposta;
private String txCelulaDestinoResposta;
private Character indAtivo;
@ManyToOne
@JoinColumn(name = "idUsuarioUltAtualizacao")
private Usuario usuarioUltAtualizacao;
private LocalDateTime dtHoraInclusao;
private LocalDateTime dtHoraUltAtualizacao;
@ManyToOne
@JoinColumns({
@JoinColumn(name = "idPerguntaClonada" ,referencedColumnName = "idConfigPergunta", insertable = true, updatable = true),
@JoinColumn(name = "idTipoDoctoEntrada", referencedColumnName = "idTipoDoctoEntrada", insertable = true, updatable = true)
})
private ConfigPergunta configPerguntaClonada;
private Character indPerguntaSuperAdmin;
错误:
Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Column 'idTipoDoctoEntrada' is duplicated in mapping for entity 'com.provider.fatoria.model.ConfigPergunta' (use '@Column(insertable=false, updatable=false)' when mapping multiple properties to the same column)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-6.1.13.jar:6.1.13]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.1.13.jar:6.1.13]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366) ~[spring-orm-6.1.13.jar:6.1.13]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-6.1.13.jar:6.1.13]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1802) ~[spring-beans-6.1.13.jar:6.1.13]
... 106 common frames omitted
Caused by: org.hibernate.MappingException: Column 'idTipoDoctoEntrada' is duplicated in mapping for entity 'com.provider.fatoria.model.ConfigPergunta' (use '@Column(insertable=false, updatable=false)' when mapping multiple properties to the same column)
at org.hibernate.mapping.Value.checkColumnDuplication(Value.java:196) ~[hibernate-core-6.5.3.Final.jar:6.5.3.Final]
at org.hibernate.mapping.MappingHelper.checkPropertyColumnDuplication(MappingHelper.java:251) ~[hibernate-core-6.5.3.Final.jar:6.5.3.Final]
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:939) ~[hibernate-core-6.5.3.Final.jar:6.5.3.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:694) ~[hibernate-core-6.5.3.Final.jar:6.5.3.Final]
at org.hibernate.mapping.RootClass.validate(RootClass.java:276) ~[hibernate-core-6.5.3.Final.jar:6.5.3.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:496) ~[hibernate-core-6.5.3.Final.jar:6.5.3.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:280) ~[hibernate-core-6.5.3.Final.jar:6.5.3.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:457) ~[hibernate-core-6.5.3.Final.jar:6.5.3.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1506) ~[hibernate-core-6.5.3.Final.jar:6.5.3.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:75) ~[spring-orm-6.1.13.jar:6.1.13]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:390) ~[spring-orm-6.1.13.jar:6.1.13]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.1.13.jar:6.1.13]
... 110 common frames omitted
我希望能够运行应用程序并能够插入和更新
我稍微简化了你的实体,它对我有用
@Entity
@DynamicUpdate
@Setter
@Getter
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class ConfigPergunta {
@EmbeddedId
private ConfigPerguntaPk configPerguntaPk;
@ManyToOne(optional = true)
@JoinColumns({
@JoinColumn(name = "idPerguntaClonada", referencedColumnName = "idConfigPergunta", insertable = true, updatable = true),
@JoinColumn(name = "configPerguntaPk.tipoDoctoEntrada", referencedColumnName = "tipoDoctoEntrada", insertable = true, updatable = true)
})
private ConfigPergunta configPerguntaClonada;
@Embeddable
@Getter
@Setter
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
public static class ConfigPerguntaPk {
Long idConfigPergunta;
String tipoDoctoEntrada;
}
}
主要技巧是使用
configPerguntaPk.tipoDoctoEntrada
作为 name
。