可嵌入的JPA可以包含Embeddable还是可嵌入的集合? [英] Is it possible for a JPA embeddable to contain Embeddable or Collection of Embeddables?

查看:81
本文介绍了可嵌入的JPA可以包含Embeddable还是可嵌入的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑是否有可能在另一个Embeddable内嵌入一个ElementCollection?



下面是我的供应商实体的一个例子,它有一个地址列表,它是可嵌入类型:

  @Entity 
公共类供应商扩展BaseCommonEntity {
@Column(unique = true)
private String supplierCode;

私人字符串supplierName;

@ElementCollection
私人列表<地址>地址;
....






我的嵌入式地址包含可嵌入电话列表

  @Embeddable 
公共类地址{
private String地址;
私人字符串城市;
私人字符串国家;
私人字符串邮政编码;

@ElementCollection
私人列表<手机>手机;
公共列表<手机> getPhones(){
返回手机;
}

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






这里是可嵌入的电话定义

  @Embeddable 
公共类电话{
private long phoneCountryCode;
private long phoneCityCode;
私人长话机号码;
private long faxCountryCode;
私人长途faxCityCode;
私人长传真号码;
...






目前正在测试这与JPA 2 + Hibernate 3.6.x



在Address中使用可嵌入的Phone将导致我的测试出错。
以下是最底部的部分调试消息:

  DEBUG PropertyBinder  - 构建属性国家
DEBUG Ejb3Column - 绑定列:Ejb3JoinColumn {logicalColumnName ='null',referencedColumn ='null',mappedBy =''}
DEBUG Ejb3Column - 绑定列:Ejb3Column {table = org.hibernate.mapping.Table(Supplier_addresses),mappingColumn = phone,insertable = true,updatable = true,unique = false}
DEBUG Ejb3Column - 绑定列:Ejb3Column {table = org.hibernate.mapping.Table(Supplier_addresses),mappingColumn = null,insertable = true,updatable = true ,unique = false}
DEBUG Ejb3Column - 绑定列:Ejb3Column {table = org.hibernate.mapping.Table(Supplier_addresses),mappingColumn = phones,insertable = true,updatable = true,unique = false}
DEBUG Ejb3Column - 绑定列:Ejb3Column {table = org.hibernate.mapping.Table(Supplier_addresses),mappingColumn = phones_KEY,insertable = true,updatable = true,unique = false}
DEBUG Ejb3Column - Bin ding列:Ejb3JoinColumn {logicalColumnName ='phones_KEY',referencedColumn ='null',mappedBy ='null'}
DEBUG Ejb3Column - 绑定列:Ejb3JoinColumn {logicalColumnName ='null',referencedColumn ='null',mappedBy =' '}
调试Ejb3Column - 绑定列:Ejb3JoinColumn {logicalColumnName ='null',referencedColumn ='null',mappedBy =''}
DEBUG CollectionBinder - 集合角色:com.primetech.core.entity.Supplier .addresses.collection&& element.phones
DEBUG PropertyBinder - 构建物业电话
DEBUG Ejb3Column - 绑定列:Ejb3Column {table = org.hibernate.mapping.Table(Supplier_addresses),mappingColumn = postcode,insertable = true,updatable = true,unique = false}
DEBUG PropertyBinder - 使用lazy = false绑定属性postcode
DEBUG SimpleValueBinder - 为邮编编制SimpleValue
DEBUG SimpleValueBinder - 为邮编设置SimpleValue typeName
DEBUG PropertyBinder - 建立属性邮编
DEBUG CollectionSecondPass - 映射的集合键:Supplier_id,元素:地址,城市,国家,邮政编码
DEBUG DefaultListableBeanFactory - 检索bean的依赖bean'org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter#102799c':[entityManagerFactory] ​​
INFO DefaultListableBeanFactory - 破坏org.springframework.beans.factory.support.DefaultListableBeanFactory@f6ac0b中的单例:定义bean [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.AnnotationTrans actionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor];工厂层次结构的根
线程main中的异常org.springframework.beans.factory.BeanCreationException:在类路径资源[test.xml]中定义的名为'entityManagerFactory'的bean创建时出错:init方法的调用失败;嵌套的异常是org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean中的java.util.ConcurrentModificationException
(AbstractAutowireCapableBeanFactory.java:1412)$ or $ $
在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.spr ingframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
处org.springframework org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
。 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)
引起:java。 util.ConcurrentModificationException java.util.AbstractList中的
$ Itr.checkForComodification(AbstractList.java:372)$ java.util.AbstractList中的
$ Itr.remove(AbstractList.java:357)
at org.hibernate.cfg.Configuration.origi nalSecondPassCompile(Configuration.java:1687)
在org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1393)
在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)
在org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:685)
在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更多

删除此部分即可解决问题

  / ****从代码中删除所有这些以解决问题**** / 
@ElementCollection
private List< Phone>手机;
公共列表<手机> getPhones(){
返回手机;
}

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






请分享您的想法!



谢谢! 解决方案

JPA 2规范说不。 / p>

JSR-317

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

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

I wonder whether it is possible to have an ElementCollection of Embeddable inside another Embeddable ?

Here's an example of my Supplier entity which has a list of Addresses, which is of an embeddable type :

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

    private String supplierName;

    @ElementCollection
    private List<Address> addresses;
....


And here's my embeddable Address that contains a list of embeddable Phone

@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;
    }
...


And here's the embeddable phone definition

@Embeddable
public class Phone {
    private Long phoneCountryCode;
    private Long phoneCityCode;
    private Long phoneNo;
    private Long faxCountryCode;
    private Long faxCityCode;
    private Long faxNo;
...


Im currently testing this with JPA 2 + Hibernate 3.6.x

Using the embeddable Phone inside the Address results in error in my test. Here's the partial debug messages from the most bottom :

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

Removing this part solves the problem

    /**** 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;
    }


Please share your thoughts !

Thank you !

解决方案

JPA 2 spec says no.

JSR-317

2.6 Collections of Embeddable Classes and Basic Types

An embeddable class (including an embeddable class within another embeddable class) that is contained within an element collection must not contain an element collection, nor may it contain a relationship to an entity other than a many-to-one or one-to-one relationship

这篇关于可嵌入的JPA可以包含Embeddable还是可嵌入的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆