gpt4 book ai didi

java - 带有外键和枚举的 JPA 复合键

转载 作者:行者123 更新时间:2023-11-30 11:11:59 24 4
gpt4 key购买 nike

大家好

我正在尝试从遗留数据库构建所有 jpa 实体。我有一个反复出现的问题:一些“主要”实体有一个“翻译”集合,其中主键是“主要”+“语言”标识符的键。语言存储在数据库和枚举中以简化处理。

我使用的 JPA 实现是 Hibernate 4。

下面是当前的实现:

主要:(消息)

@Entity
@Table(name="MESSAGES")
@NamedQueries({
public class Message implements Serializable {

@Id
@SequenceGenerator(name="MESSAGES_MESSAGEID_GENERATOR", sequenceName="SEQ_MSG_messageID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="MESSAGES_MESSAGEID_GENERATOR")
@Column(name="messageId")
private long id;

@OneToMany(mappedBy="message", fetch=FetchType.EAGER)
private Set<MessageDesc> descriptions;
}

翻译:(MessageDesc)

@Entity
@Table(name="MESSAGEDESCS")
public class MessageDesc implements Serializable, Translatable {

@EmbeddedId
private MessageDescPK id;

@NotNull
@Enumerated(EnumType.ORDINAL)
@Column(name = "LANGUAGEID")
private LanguageEnum language;

@NotNull
@ManyToOne
@JoinColumn(name="MESSAGEID")
private Message message;
}

翻译复合键:(MessageDescPK)

@Embeddable
public class MessageDescPK implements Serializable {

@Column(name="messageid", nullable = false, insertable = false)
private long message;

@Column(name="languageid", nullable = false, insertable = false)
private int language;
}

语言:(语言枚举)

public enum LanguageEnum {
FRENCH(0, "FR"),
DUTCH(1, "NL");

private int id;
private String desc;
}

问题是,当尝试保留一个主(消息)实体时,在描述中包含一些翻译以及设置了语言和消息的翻译,我不断收到以下异常:

org.springframework.orm.jpa.JpaSystemException: org.hibernate.id.IdentifierGenerationException: null id generated for:class something.persistence.entity.message.MessageDesc; nested exception is javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: null id generated for:class something.persistence.entity.message.MessageDesc
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:321)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:111)

我不明白为什么在 messageDesc 实例上同时设置语言和消息(已经保留)后 PK 为空。任何帮助、评论、线索将不胜感激。

谢谢!

最佳答案

这就是我们解决这个问题的方法:

消息描述:

@NotNull    
@Enumerated(EnumType.ORDINAL)
@Column(name = "LANGUAGEID", insertable=false, nullable = false)
private LanguageEnum language;

@NotNull
@ManyToOne
@JoinColumn(name="MESSAGEID", insertable=false, nullable = false)
private Message message;

@PrePersist
private void prePersist(){
if (getId() == null){
MessageDescPK id = new MessageDescPK();
//the way this enum is persisted is EnumType.ORDINAL
id.setLanguage(getLanguage().ordinal());
id.setMessage(getMessage().getId());
this.setId(id);
}
}

MessageDescPk:

@Embeddable
public class MessageDescPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;

@Column(name="messageid", nullable = false, insertable = false, updatable=false)
private long message;

@Column(name="languageid", nullable = false, insertable = false, updatable=false)
private int language;

希望这有帮助:)

关于java - 带有外键和枚举的 JPA 复合键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27124152/

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