简体   繁体   中英

Change the Entity ID name in JPA

Could be possible change the name of the Entity variable ID in JPA? Currently, the DAO save method use the Entity ID. We have multiple existing tables in our database and then, we will have to add the ID column to all tables. Is there any way I can avoid adding the fields? I suppose one possibility will be to override the DAO methods, but any other ideas? Thanks in advance.

So your entity is defined as:

@Entity
public class SomeEntity {
  @Id
  private Integer id;
  //...
}
  1. You don't have to name the variable id unless you really want to. You could name this someEntityId and JPA nor Hibernate cares. The only influence the property name will have is how the NamingStrategy implementation will map that to a column name in the database.

  2. We can override the naming strategy and apply specific names to columns if we need where cases exist that the naming strategy doesn't work for us.

     @Id @Column(name = "some_entity_id") private Integer id; 

    This essentially will map the id variable to a column named some_entity_id .

Hope that helps.

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