package com.xenoterracide.rpf.model.abstracts;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
import org.springframework.data.domain.Persistable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotNull;
import java.util.UUID;
@MappedSuperclass
public abstract class AbstractEntityBase extends AbstractPersistable<UUID> implements Persistable<UUID> {
// AbstractPersistable is a Copy of the Spring Data JPA version testing to see if I can fix this problem,
// also bug DATAJPA-622, plan to implement the spring variant if I can get it working with UUID
@NotNull
@Override
@Generated( GenerationTime.ALWAYS)
@GeneratedValue(generator = "entityIdGenerator")
@Column( columnDefinition = "uuid", updatable = false)
public UUID getId() {
return super.getId();
}
}
这是期末课程
@Entity
@Table(name = "characters")
class Character extends AbstractEntityBase {
String name;
@Override
@Column( name = "character_id")
public UUID getId() {
return super.getId();
}
但是我得到这个错误
Caused by: org.hibernate.MappingException: Duplicate property mapping of id found in com.xenoterracide.rpf.model.character.Character
at org.hibernate.mapping.PersistentClass.checkPropertyDuplication(PersistentClass.java:515)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:505)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1358)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
如何配置
AbstractPersistable
的ID以正确执行UUID并拥有所需的列名?在使用AbstractPersistable
的同时最好不要在我的最后一堂课上重复注释更新我已经设法通过
@AttributeOverride
进行了一些更新,但是实际上我离AbstractPersistable和最终类之间没有一个类(或者在最终类的外部)差不多,该类可以正确地进行uuid生成。尝试将@GenericGenerator
移至我的package-info.java
,但随后它停止通过该名称识别生成@Entity
@Table(name = "characters")
@GenericGenerator( name = "uuid-generator", strategy = "uuid2" )
@AttributeOverride(
name = "id",
column = @Column(
name = "character_id",
insertable = false,
updatable = false,
unique = true,
nullable = false
) )
class Character extends AbstractPersistable<UUID> implements Persistable<UUID> {
String name;
protected Character(final String name) {
this.name = name;
}
@SuppressWarnings( "unused" )
protected Character() {
}
@Id
@Override
@Generated( GenerationTime.ALWAYS )
@GeneratedValue( generator = "uuid-generator" )
public UUID getId() {
return super.getId();
}
@NotNull
@SuppressWarnings( "unused" )
protected String getName() {
return name;
}
@SuppressWarnings( "unused" )
protected void setName(String name) {
this.name = name;
}
}
最佳答案
您可以在最终课程中覆盖ID:
@Override
@Column( name = "character_id")
public UUID getId() {
return super.getId();
}
您可以让继承来做!因此,您的最后一堂课必须是这样的:
@Entity
@Table(name = "characters")
class Character extends AbstractEntityBase {
String name;
}
getId()从AbstractEntityBase类继承。您将拥有一个uuid列和一个名称。
更新:
从Spring开始:“ AbstractPersistable是一站式商店,提供非常基本的用例。它唯一要做的就是设置默认ID生成。如果要自定义ID,则扩展类不会有任何好处。”
因此,您的类AbstractEntityBase需要直接实现Persistable,这将纠正您的问题。
@MappedSuperclass
public abstract class AbstractEntityBase implements Persistable<UUID> {
@Generated( GenerationTime.ALWAYS)
@GeneratedValue(generator = "entityIdGenerator")
@Column( columnDefinition = "uuid", updatable = false)
private UUID id;
// implements methods like hashcode or equals
....
}