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.
-
1JPA does not force you to any particular naming strategy, so you don't need a variable called ID in your entities. You do need some unique way to identify your entities though. Maybe show more specifics of your problem if you need specific solutions.– ChrisMar 1, 2017 at 18:18
1 Answer
So your entity is defined as:
@Entity
public class SomeEntity {
@Id
private Integer id;
//...
}
You don't have to name the variable
id
unless you really want to. You could name thissomeEntityId
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.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 namedsome_entity_id
.
Hope that helps.