JPA column from another table

1482
June 23, 2018, at 07:20 AM

I have a very old database. I need to implement JPA mapping for this database and unfortunately I can't change the existing structure of this database at all.

Let's say that I have two tables (in fact there are more, but let's consider only two for simplicity) common and booking

common:
- id - unique primary key
- type
- description
booking:
- id - unique primary key
- category_id - this is NOT unique

I'm creating the following mapping for these tables.

@Entity
public class Common {
  @Id
  private Long id;
  private String type;
  private String description;
  // getters setters ommited   
}
@Entity
public class Booking {
  @Id
  private Long id;
  private Long categoryId;
  // value for this field should be taken from common table 
  private String description;
  // getters setters ommited   
}

As you can see, the problem is that I need to have a field called description in Booking entity. The value for this field should be taken from the Common entity's description field using the following condition - booking.category_id = common.id and common.type = 'booking'.

I know that it looks a bit strange, but I need to implement it in some way. So far I have found only one approach that I could've used which is a @SecondaryTable annotation.

This annotation is used when you need to map your entity to multiple tables, which is kind of similar to my case.

I tried to apply it in the following way:

@Entity
@SecondaryTable(name = "common", pkJoinColumns = 
@PrimaryKeyJoinColumn(name="id", referencedColumnName="categoryId"))
public class Booking {
  @Id
  private Long id;
  private Long categoryId;
  @Column(name = "description", table = "common")
  private String description;
  // getters setters ommited   
}

but it doesn't work since categoryId is not unique and leads to the following error

Caused by: org.hibernate.AnnotationException: SecondaryTable JoinColumn cannot reference a non primary key

Moreover, I still need to specify that description common.type = booking and I don't know if it is possible to specify it via @SecondaryTable.

In fact, there is another approach to do it - via Hibernate @Formula annotation.

@Formula("select c.description from Common c where c.id = categoryId and c.type = 'booking'")
private String description;

it should work, but I don't really like this approach cause it's Hibernate specific and looks like a bad solution for me, since @Formula is not intended for this.

The last solution is to declare @OneToOne relationship between Common and Booking entities which is also a bad solution for me, because I have too many tables, where some data should be also taken from this common table and I wouldn't want to wire them all with common table.

For now, common table is like a big garbage table in this database, but I still need to use it in order to retrieve some values from it.

Any help would be appreciated. Thanks in advance.

Answer 1

you want a SingleTable Strategy. you will create other objects that inherit from Common. Give type as the discriminator collumn. Create object Description and use that on your booking object instead of string. then use @OneToOne or @OneToMany mapping from Booking to Description. then no special queries are needed.

Rent Charter Buses Company
READ ALSO
How do I compare strings in Java?

How do I compare strings in Java?

I've been using the == operator in my program to compare all my strings so farHowever, I ran into a bug, changed one of them into

189
Redis connection check

Redis connection check

Is it any difference between check

168
Unable to set property 'Headers' of undefined or null reference

Unable to set property 'Headers' of undefined or null reference

I have a react/node/webpack app that is working fine on Chrome and Firefox, however I am getting a blank screen on Internet Explorer 11I thought this would be an easy fix by including "babel-polyfill" in the webpack entry point like this:

196
HTML File src/link file paths not working

HTML File src/link file paths not working

I have an html file system that currently goes like this:

39