How to map two JPA or Hibernate entities on the same database table How to map two JPA or Hibernate entities on the same database table database database

How to map two JPA or Hibernate entities on the same database table


To summarize it, the following mappings are going to demonstrate how you can map multiple entities to the same database table:

@Entity(name = "Post")public class Post {    @Id    @GeneratedValue(strategy=GenerationType.AUTO)    private Long id;    private String name;    private String description;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getDescription() {        return description;    }    public void setDescription(String description) {        this.description = description;    }}@Entity(name = "PostSummary")@Table(name = "Post")@Immutablepublic class PostSummary {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private Long id;    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}@Entity(name = "UpdatablePostSummary")@Table(name = "Post")@DynamicUpdatepublic class UpdatablePostSummary {    @Id    @GeneratedValue(strategy = GenerationType.AUTO)    private Long id;    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

And Hibernate will work just fine:

@Testpublic void testOneTableMultipleEntities() {    doInTransaction(session -> {        Post post = (Post) session.get(Post.class, 1L);        PostSummary postSummary = (PostSummary) session.get(PostSummary.class, 1L);        UpdatablePostSummary updatablePostSummary = (UpdatablePostSummary) session.get(UpdatablePostSummary.class, 1L);        assertEquals(post.getName(), postSummary.getName());        assertEquals(post.getName(), updatablePostSummary.getName());        updatablePostSummary.setName("Hibernate Master Class Tutorial.");    });}
  1. The PostSummary is just a read-only View over your original entity, hence I annotated it with @Immutable.

  2. The UpdatablePostSummary is marked with @DynamicUpdate and so you can propagate changes from this View entity too.

This test is also available on GitHub.


I have a similar Problem. I have two classes, the base class DocumentContent and a derived class DocumentContentWithData:

DocumentContent {    private Long documentContentId;    private InputStream contentStream;    private File importFile;    ... and several other attributes}DocumentContentWithData extends DocumentContent {}

I defined a hibernate mapping for the two classes, for the class DocumentContent a mapping without the contentStream and for the class DocumentContentWithData a mapping with the contentStream.

Saving the data with the class DocumentContentWithData works and also getting the instances for both classes works with the method Session.get(Class, ID).

However the query in the following code returns two entities, one as instance of DocumentContent and the other as instance of DocumentContentWithData although there is only one entry with the importFile in the database:

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();CriteriaQuery<DocumentContent> criteriaQuery = criteriaBuilder.createQuery(DocumentContent.class);Root<DocumentContent> root = criteriaQuery.from(DocumentContent.class);criteriaQuery.select(root).where(criteriaBuilder.equal(root.get("importFile"), importFile));Query<DocumentContent> query = session.createQuery(criteriaQuery);List<DocumentContent> contentList = query.getResultList();

Can someone explain this behavior. When I do not derive the class DocumentContentWithData from DocumentContent and duplicate all attributes it works. However this is not a very nice solution.


You have to add annotation @Entity,@Table(name="RESTAURANT") on your class, add annotations and replace your detailed mapping in hbm file by
.

Here a complete example: http://viralpatel.net/blogs/hibernate-many-to-many-annotation-mapping-tutorial/