clougioc clougioc - 4 months ago 15
Java Question

How to generate name of ID in base object in Hibernate

I have base object for all my entity:

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


The problem is, that I need unicate name for id.

Like this:

user_id, car_id, cat_id


I dont know, how to do it, can you help me?

Its possible override anotation
Column(name = "entity_id")
in child class?

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

Answer

Yes... you need to use @AttributeOverride annotation on each subclass. For example,

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