简体   繁体   中英

Primary Key Column Name with Spring Data JPA and Hibernate

I have the following setup with Spring Data JPA and Hibernate as the persistence provider. All of my entities inherit from a base class

@MappedSuperclass
public class BaseEntity {

  @Id  
  private id;

  @Version
  private String version;

  //more common fields
}

For example:

@Entity
public class Foo extends BaseEntity {

}

This leads to a primary key column with name "ID" to be generated on the "FOO" table. I would like to change the naming of the primary key column. It should reflect the name of class or table. So it should be "FOO_ID" instead of just "ID".

I know that I could do this statically by using @Column(name = "FOO_ID"). But that would mean I have to do this for every Entity. Is there a more dynamic way to achieve this?

由于继承,您的所有子类都将具有相同的ID列名称,您可以为基本实体类中的所有子类指定一个通用的ID列名。

  1. Why use inheritance then? Just do it without inheritance.

  2. You could use getters/setters to rename your fields

Ex:

class Foo {

  private Long id;

  public Long getFooId() {
    return this.id;
  }

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

I know this is an old question, but stumbled across this looking for an answer... Eventually found this solution elsewhere:

@Entity
@AttributeOverride(name="id", column=@Column(name="FOO_ID"))
public class Foo extends BaseEntity {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM