我通过第三个表在两个表之间有多对多的关系:
TABLE_A A_TO_B TABLE_B
--------- ------------- ------------
KEY (PK) KEY_A (PK, FK) KEY (PK)
--some more Atributes KEY_B (PK, FK) START_DATE (PK)
--some Attributes END_DATE
--some Attributes
正如您所看到的,此表结构的问题是Table B
具有包含条目的startDate的组合主键。这是为了能够有入门版本。我无法更改表结构。我想通过TABLE_A
属性加入JPA中的TABLE_B
和@ManyToMany
。但JPA要求,A_TO_B
知道TABLE_B
的完整主键。但我完全满意地忽略了START_DATE
,并且只需要TABLE_B
的所有KEY_B
条目用于特定的KEY_A
。
这是错误:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring_hibernate_configuration.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: referencedColumnNames(KEY_B) of de.mycompany.entity.model.TableA.listOfBs referencing de.mycompany.entity.model.TableB not mapped to a single property
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at de.mycompany.Start.main(Start.java:14)
Caused by: org.hibernate.AnnotationException: referencedColumnNames(KEY_B) of de.mycompany.entity.model.TableA.listOfBs referencing de.mycompany.entity.model.TableB not mapped to a single property
at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:320)
at org.hibernate.cfg.annotations.CollectionBinder.bindManytoManyInverseFk(CollectionBinder.java:1680)
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1560)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:800)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:725)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 11 more
这是我的代码:
@Entity
@Table(name = "TABLE_A")
public class TableA {
@Id
@Column(name = "KEY", nullable = false)
private int key;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "A_TO_B",
joinColumns = @JoinColumn(name = "KEY_A",
referencedColumnName = "KEY"),
inverseJoinColumns = @JoinColumn(name = "KEY_B",
referencedColumnName = "KEY"))
private List<TableB> listOfBs;
/* More Fields, Getters and setters*/
}
@Entity
@Table(name = "TABLE_B")
public class TableB {
@EmbeddedId
private TableBId id;
@Column(name = "END_DATE", nullable = false)
private LocalDate endDate;
@ManyToMany(mappedBy = "listOfBs")
private List<TableA> listOfAs;
/* More fields, Getters and Setters*/
}
public class TableBId implements Serializable {
@Column(name = "KEY", nullable = false)
private int key;
@Column(name = "START_DATE", nullable = false)
private LocalDate startDate;
/* Getters and Setters*/
}
@Entity
@Table(name = "A_TO_B")
public class AtoB {
@EmbeddedId
private AtoBId id;
/* Getters and Setters*/
}
public class AtoBId implements Serializable {
@Column(name = "KEY_A", nullable = false)
private int keyA;
@Column(name = "KEY_B", nullable = false)
private int keyB;
/* Getters and Setters */
}
当我用另一个包含日期的假连接列定义@ManyToMany
时,应用程序启动就好了。
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "A_TO_B",
joinColumns = @JoinColumn(name = "KEY_A",
referencedColumnName = "KEY"),
inverseJoinColumns = {@JoinColumn(name = "KEY_B",
referencedColumnName = "KEY"),
@JoinCokumn(name = "SOME_DATE",
referencedColumnName = "START_DATE")})
private List<TableB> listOfBs;
但这不会给我我想要的结果。我只想加入A_TO_B
and TABLE_B
而不是KEY_B
。
对于有类似问题的人来说,这就是我最后所做的:
我在A瞬态中创建了B列表,这意味着它没有保存在DB中。然后我通过getter懒惰地填写它:
@Entity
@Configurable
@Table(name = "TABLE_A")
public class TableA {
@Id
@Column(name = "KEY", nullable = false)
private int key;
@Transient
private List<TableB> listOfBs;
@Transient
@Autowired
AtoBRepository aToBRepository;
@Transient
@Autowired
TableBRepository tableBRepository;
public List<TableB> getListOfBs(){
if (listOfBs == null){
listOfBs = new ArrayList<>();
List<AtoB> AtoBs = aToBRepository.findByIdKeyA(key);
for (AtoB aToB : AtoBs){
listOfBs.addAll(tableBRepository.findByIdKey(aToB.getKeyB));
}
}
return listOfBs;
}
/* More Fields, Getters and setters*/
}
要在实体中自动装配存储库,我按照此主题中的描述加载时间编织:load Time weaving with @configurable