jpa,eclips-link 2.5.1:OneToMany不适用于非主键的列 [英] jpa, eclips-link 2.5.1: OneToMany not working on columns not primary key

查看:114
本文介绍了jpa,eclips-link 2.5.1:OneToMany不适用于非主键的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个实体:

无花果

  @Entity
    @Access(AccessType.FIELD)
    @Table(name = "S_MC_CC_USER")
    @SequenceGenerator(name = "SEQ_ID", sequenceName = "SEQ_ID", allocationSize = 1)
    public class Anagrafica implements Serializable{
        private static final long serialVersionUID = 332466838544720886L;

        @EmbeddedId
        private AnagraficaId anagraficaId;

        @Column(name = "USER_ID")
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_ID")
        private Long userId;

        @OneToMany(cascade = CascadeType.ALL)
        @JoinColumn(name = "USER_ID", updatable = false, insertable = false)
        private List<Mobile> mobiles;

/**
     * La classe di dominio che modella la chiave primaria di un {@link Anagrafica}
     * 
     * @author Massimo Ugues
     * 
     */
    @Embeddable
    static public class AnagraficaId implements Serializable {
        private static final long serialVersionUID = -54640203292300521L;

        @Column(name = "ANAG_UTENTE")
        private String bt;

        @Column(name = "COD_ABI")
        private String abi;

        public AnagraficaId() {
            super();
        }

移动

@Entity
@Table(name = "S_MOBILE")
@SequenceGenerator(name = "SEQ_MOBILE", sequenceName = "SEQ_MOBILE", allocationSize = 1)
public class Mobile implements Serializable{
    private static final long serialVersionUID = 5999493664911497370L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_MOBILE_DEVICE_REGISTRY")
    @Column(name = "ID_MOBILE")
    private Long mobileId;

    @Column(name = "DEVICE_TOKEN")
    private String deviceToken;

    @Column(name = "DATA_INSERIMENTO")
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar dataInserimento = Calendar.getInstance();

使用eclispe-link 2.1.2都可以很好地工作,但是使用eclispe-link 2.5.1时,我遇到了以下异常:

With eclispe-link 2.1.2 all works great, but with eclispe-link 2.5.1 I got this exception:

Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [persistence-unit] failed.
Internal Exception: Exception [EclipseLink-7220] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The @JoinColumns on the annotated element [field mobiles] from the entity class [class com.intesasanpaolo.domain.entities.sub.Anagrafica] is incomplete. When the source entity class uses a composite primary key, a @JoinColumn must be specified for each join column using the @JoinColumns. Both the name and the referencedColumnName elements must be specified in each such @JoinColumn.
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1954)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1945)
    at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactory(PersistenceProvider.java:322)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:288)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
    ... 40 more

问题是基于不是主键的外键的OneToMany关联. 由于无法更改数据库模型,我该如何使其工作?

The problem is the OneToMany association based on a foreign key that is not primary key. Since I cannot change the database model how can I make it work?

亲切的问候 马西莫

推荐答案

在较早版本中工作的原因是EclipseLink不会查看映射中的字段,但是随着JPA添加了派生的Id支持,现在EclipseLink验证外键的数量与ID字段的数量匹配.

The reason it worked in a prior version was that it EclipseLink doesn't look at the fields in the mapping, but with JPA adding derived Id support, EclipseLink now validates the number of foreign keys match the number of ID fields.

詹姆斯的回答在这里 在非主键列上连接时的JPA @JoinColumn问题 向您说明,您将需要使用描述符定制器来更改JPA映射.因此,您要么不映射JPA中的字段(将其标记为@Transient),然后在定制程序中添加映射,要么让JPA映射使用所有主键字段,然后将其更改为仅使用USER_ID的定制器中的映射-> USER_ID字段.

James' answer here JPA @JoinColumn issues while joining on non primary key columns explains it that you'll need to use a descriptorCustomizer to change the JPA mapping. So you would either not map the field in JPA (mark it as @Transient) and then add a mapping in the customizer, or have the JPA mapping to use all primary key fields and then change the mapping in the customizer to only use the USER_ID->USER_ID fields.

EclipseLink定制器如下所示: http://eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_customizer.htm

EclipseLink customizers are shown here: http://eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_customizer.htm

这篇关于jpa,eclips-link 2.5.1:OneToMany不适用于非主键的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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