如何在Hibernate中生成基础对象中的ID名称

时间:2016-07-23 19:39:26

标签: java hibernate

我的所有实体都有基础对象:

@MappedSuperclass
public class BaseObject {

    @Id
    @GeneratedValue
    @Column(name = "entity_id")
    private Long id;

    /**
     *
     * @return true if the entity hasn't been persisted yet
     */
    @Transient
    public boolean isNew() {
        return id == null;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

问题是,我需要id的统一名称。

像这样:

user_id, car_id, cat_id

我不知道,怎么做,你能帮助我吗?

在子类中可能覆盖anotation Column(name = "entity_id")吗?

public class User extends BaseObject
public class Car extends BaseObject
public class Cat extends BaseObject

1 个答案:

答案 0 :(得分:0)

是的......您需要在每个子类上使用@AttributeOverride注释。例如,

@Entity
@AttributeOverride(name="id", column=@Column(name="user_id"))
public class User extends BaseObject { ... }