Customized naming of columns in JPA relationships

When I make relationsships with JPA using hibernate, some terrible long and ackward column names are generated.
Eg. I have the following actionPlan_actionPlanPK, which means that the column actionPlan is a FK that points to actionPlanPK.
To make it look just a little bit more neat in the DB I would like if I could give it a name myself, preferably just the name it has in the entity class that owns the relationship.

Is this possible with JPA?

Asked by: John502 | Posted: 23-01-2022

Answer 1

Yes. It is possible to specify your own name for a column if you are not satisfied with the default names. For instance in the class with the reference to the ActionPlan you can specify:

public ActionPlan getActionPlan(){


And thus, the column name will be "actionplanid".

Answered by: Tara984 | Posted: 24-02-2022

Answer 2

This capability is part of the JPA specification and allows for the naming of many of your database structures in the annotations. These include:

Naming your table as follows:

public class MyConvolutedClassName {

Naming your columns as follows:

private Date myConvolutedDateColumn;

Naming your columns that are part of a relationship:

private ClassName otherModelClass;

There's a great (though not quite up-to-date) cheatsheet for EJB 3.0 annotations (which includes JPA) available at

Answered by: Anna910 | Posted: 24-02-2022

Similar questions

java - How to implement one-to-many relationships in Ibatis?

Let's say I have this Class: Class A { int id; int[] b; // Other properties } Class B { int id; // Other properties } The Class A has one-to-many relation with class B. I already have a service which caches B objects and return them on id. Table schema looks something like this Table a: ------- ...

java - Hibernate Auditable many-to-many relationships

Consider the following hibernate configuration: <class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <set name="addresses" table="PersonAddress"> <key column="personId"/> <many-to-many column="addressId" class="Address"/> </set> </class> <class name="Address"> <id name="id" column="address...

java - JAXB and multiple object relationships

We are using Jersey (Java REST library) for a project for the last few months and loving it. But this week have run into an issue with JAXB. What I have is an element that has 2 children, each of them have children where some of their children reference each other. Let me show some code. Root root = new Root(); Parent parent1 = new Parent(); Parent parent2 = new Parent(); root.add(paren...

java - JPA mapping: Reusing entities in multiple relationships

Let's say I have: class Unit { private TextContainer source; private List<TextContainer> targets; } Can I annotate class TextContainer in such a way that it works within both relationships? TextContainer must be either source or target.

java - Mapping Relationships with Hibernate

I'm still learning how to set up these mappings using the JPA annotations so this may be a beginner mistake. I have a User class and a Course class. In the database each Course has a User foreign key in each row (think of the User as the teacher teaching the Course). So each User can have multiple Courses, but each Course has only one User. In mapping these classes I have written the following code below:

java - How to implement One-to-Many mapping when the associative table is one for all one-many relationships?

General case: first table represents one-side, second table represents many-side. Third table serves as a link between the two. My case: first and second tables are the same. Third table serves as a link between all pairs of tables which have one-to-many relationship. This third table has additional field (String) which contains information about 2 tables. Little example. Suppose we have...

java - How do I declare a member with multiple generic types that have non-trivial relationships?

Here is what I would like to write in my java code: private <A extends Action<R>, R extends Result> MyType<A,R> member; This is invalid syntax however. So I end up writing: private MyType<? extends Action<? extends Result>, ? extends Result> member; But this disregard the fact that both classes derived from Result...

java - Mapping parent-child relationships with iBatis

I have the classic setup public class Parent { Integer id; ... // No generics Collection someCollectionAttribute; ... public void setChildren(Collection c) { ... } } public class Child { Integer id; ... } and I'm trying to map this on the usual table setup using iBatis (version 2.30... don't ask). create table parents ( ID in...

java - Modeling multiple polymorphic relationships using Hibernate

Ruby on Rails has polymorphic relations which are really useful for implementing functionality such as commenting, tagging and rating to name a few. We can have a comment, tag or rating class which has a many to one polymorphic relationship with a commentable, taggable and rateable object. Also, a given domain object can choose to implement any combination of such relations. So, it can for example be commentable, taggable...

java - JPA Problems mapping relationships

I have a problem when I try to persist my model. An exception is thrown when creating the EntityManagerFactory: Blockquote javax.persistence.PersistenceException: [PersistenceUnit: ASD] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory( at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(Hibe...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)