gpt4 book ai didi

hibernate - JPA embeddable 是否可以包含 Embeddable 或 Embeddable 集合?

转载 作者:行者123 更新时间:2023-12-02 23:04:12 29 4
gpt4 key购买 nike

我想知道是否可以在另一个 Embeddable 中包含一个 Embeddable 的 ElementCollection ?

这是我的供应商实体的示例,其中包含可嵌入类型的地址列表:

@Entity
public class Supplier extends BaseCommonEntity {
@Column(unique=true)
private String supplierCode;

private String supplierName;

@ElementCollection
private List<Address> addresses;
....
<小时/>

这是我的可嵌入地址,其中包含可嵌入电话列表

@Embeddable
public class Address {
private String address;
private String city;
private String country;
private String postcode;

@ElementCollection
private List<Phone> phones;
public List<Phone> getPhones() {
return phones;
}

public void setPhones(List<Phone> phones) {
this.phones = phones;
}
...
<小时/>

这是可嵌入手机的定义

@Embeddable
public class Phone {
private Long phoneCountryCode;
private Long phoneCityCode;
private Long phoneNo;
private Long faxCountryCode;
private Long faxCityCode;
private Long faxNo;
...
<小时/>

我目前正在使用 JPA 2 + Hibernate 3.6.x 进行测试

在我的测试中,在地址中使用嵌入式电话会导致错误。这是来自最底部的部分调试消息:

DEBUG PropertyBinder - Building property country
DEBUG Ejb3Column - Binding column: Ejb3JoinColumn{logicalColumnName='null', referencedColumn='null', mappedBy=''}
DEBUG Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(Supplier_addresses), mappingColumn=phones, insertable=true, updatable=true, unique=false}
DEBUG Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(Supplier_addresses), mappingColumn=null, insertable=true, updatable=true, unique=false}
DEBUG Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(Supplier_addresses), mappingColumn=phones, insertable=true, updatable=true, unique=false}
DEBUG Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(Supplier_addresses), mappingColumn=phones_KEY, insertable=true, updatable=true, unique=false}
DEBUG Ejb3Column - Binding column: Ejb3JoinColumn{logicalColumnName='phones_KEY', referencedColumn='null', mappedBy='null'}
DEBUG Ejb3Column - Binding column: Ejb3JoinColumn{logicalColumnName='null', referencedColumn='null', mappedBy=''}
DEBUG Ejb3Column - Binding column: Ejb3JoinColumn{logicalColumnName='null', referencedColumn='null', mappedBy=''}
DEBUG CollectionBinder - Collection role: com.primetech.core.entity.Supplier.addresses.collection&&element.phones
DEBUG PropertyBinder - Building property phones
DEBUG Ejb3Column - Binding column: Ejb3Column{table=org.hibernate.mapping.Table(Supplier_addresses), mappingColumn=postcode, insertable=true, updatable=true, unique=false}
DEBUG PropertyBinder - binding property postcode with lazy=false
DEBUG SimpleValueBinder - building SimpleValue for postcode
DEBUG SimpleValueBinder - Setting SimpleValue typeName for postcode
DEBUG PropertyBinder - Building property postcode
DEBUG CollectionSecondPass - Mapped collection key: Supplier_id, element: address, city, country, postcode
DEBUG DefaultListableBeanFactory - Retrieved dependent beans for bean 'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#102799c': [entityManagerFactory]
INFO DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f6ac0b: defining beans [dataSource,entityManagerFactory,testEntities,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [test.xml]: Invocation of init method failed; nested exception is java.util.ConcurrentModificationException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.primetech.module.purchase.app.TestEntities.main(TestEntities.java:27)
Caused by: java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.remove(AbstractList.java:357)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1687)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1393)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1477)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1096)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:685)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409)
... 12 more

删除这部分可以解决问题

    /**** REMOVE ALL OF THIS FROM THE CODE TO SOLVE THE PROBLEM ****/
@ElementCollection
private List<Phone> phones;
public List<Phone> getPhones() {
return phones;
}

public void setPhones(List<Phone> phones) {
this.phones = phones;
}
<小时/>

请分享您的想法!

谢谢!

最佳答案

JPA 2 规范说不。

JSR-317

2.6 可嵌入类和基本类型的集合

包含在元素集合中的可嵌入类(包括另一个可嵌入类中的可嵌入类)不得包含元素集合,也不得包含与除多对一或一对多之外的实体的关系。一对一关系

关于hibernate - JPA embeddable 是否可以包含 Embeddable 或 Embeddable 集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5261659/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com