【发布时间】:2014-02-14 15:33:16
【问题描述】:
我有这两个实体:
意象
@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 我得到了这个例外:
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 关联。 由于我无法更改数据库模型,我该如何使其工作?
亲切的问候 马西莫
【问题讨论】:
-
您的映射使用可更新 = 假、可插入 = 假的任何原因?从显示的内容来看,这意味着在 JPA 中永远无法触及这种关系,除非某些内容映射到 Mobile 表中使用的外键字段。
标签: jpa eclipselink one-to-many