如何一般设置属性配置?

时间:2014-11-15 17:09:56

标签: java hibernate jpa spring-data

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

1 个答案:

答案 0 :(得分:2)

您覆盖最终类中的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生成。如果你想定制它,那么扩展课程就没有任何好处。” 因此,您的类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
    ....
}