4

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.

1
  • 1
    JPA 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.
    – Chris
    Mar 1, 2017 at 18:18

1 Answer 1

15

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.