Jpa, How to Use the Same Class (Entity) to Map Different Tables

How to map multiple classes to one table in JPA?

If this object orderitem is not going to expand, I would suggest JSON string saving in the database.

OR you can do following mappings:

@OneToMany(mappedBy="order")
public Order(List<Item> products) {
this.products = products;
}

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "subitem_id", referencedColumnName = "id")
private SubItems subItems;

Java Hibernate JPA create an entity and Join two different tables referenced by the same column, with same column name

The mapping for your audit Entity might be:

@Entity
@Table(name = "travel_audit")
public class TravelAudit {
@Id
@Column(name="travel_id")
private String travelId;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "travel_id", insertable=false, updatable=false)
private Travel travel;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "travel_id", insertable=false, updatable=false)
private TravelHistory travelHistory;
}

If you are using JPA/Hibernate to generate the DDL, you'll want to make sure there isn't a constraint generated for the travel_audit.travel_id -> travel table reference, as it will get broken any time your app tries to delete travel rows still referenced by a travel audit instance.

Having a basic mapping (travelId) will allow you to query using the value without having to join between the two tables and complicate your queries as the default inner join would break the logic you need.

Hibernate: Multiple tables, same object

You need:

@MappedSuperclass
public class ClassWithTheFields{
\\Not annotated with @Entity
@Id private Integer Id;
...
}

@Entity
public class EntityClass extends ClassWithTheFields{}

@Entity
public class AnotherEntityClass extends ClassWithTheFields{}

This way, both classes extending ClassWithTheFields will have the same fields, but will be mapping different tables.

You just need to put all the common fields in a class annotated with @MappedSuperclass, but not@Entity, and then extend this class in other classes annotated with @Entity.

How to map dynamically a single entity class to multiple tables?

In case it matters, I'm the author of the blog posts you mentioned.

Let's break down your question in two:

  1. is it possible to map the same class to multiple tables without subclassing?
  2. is it possible to adjust the mapping at runtime to accomodate for new tables?

The answer to 1. is: yes, if you don't use annotations-based mapping and if you map the same class under different entity names. The second link you posted (How to map one class to different tables using hibernate/jpa annotations) hints at that solution; you can do it with HBM files, or with programmatic configuration of course. You'll have to use the more verbose save(), update(), etc. overloads that allow to specify the entity name as an extra argument.

The answer to 2. is: yes and no; you can rebuild a whole new SessionFactory with the new mappings alongside the old ones (as we do in Portofino), but you cannot programmatically add new mappings to an existing SessionFactory. That means that at one point you'll have to close all existing sessions, dispose of the old SessionFactory, build the new one and start using it. In other words, yes, you can do it at runtime, but not seamlessly and concurrently with other uses of the same session factory. You can of course use multiple SessionFactories to split the mappings or to provide redundant mappings and handle gracious update schemes; it really depends on your requirements.



Related Topics



Leave a reply



Submit