1

I am using Sprind JPA, Spring 3.1.2(in future 3.2.3), Hibernate 4.1 final. I am new to Sprind Data JPA. I have tow Table Release_date_type and Cache_media which entities are as follows :

ReleaseAirDate.java

@Entity
@Table(name = "Release_date_type")
public class ReleaseDateType {
    @Id
    @GeneratedValue(strategy=GenerationType.TABLE)
    private Integer release_date_type_id;
    @Column
    private Integer sort_order;
    @Column
    private String description;
    @Column
    private String data_source_type;
    @Column(nullable = true) 
    private Integer media_Id;
    @Column
    private String source_system; with getters and setters..

and CacheMedia as

@Entity
@Table(name = "Cache_Media")
public class CacheMedia {   
    @Id
    @GeneratedValue(strategy=GenerationType.TABLE)
    private Integer id;
    @Column(name="code")
    private String code;
    @Column(name="POSITION")
    private Integer position;
    @Column(name="DESCRIPTION")
    private String media_Description; with setter and getters.

Now my repository interface is as follows :

public interface ReleaseDateTypeRepository extends CrudRepository<ReleaseDateType, Long>{ }

Now i want to write a method(Query) in ReleaseDateTypeRepository interface which can get all the data from Release_Date_Type table including appropriate media_description from Table 'Cache_Media' based on media_id of Release_date_type table.

So my select (SQL)query looks like

SELECT * from Release_Date_Type r left join Cache_Media c on r.media_id=c.id

I don't know how to map entities. I tried so many thing but not luck. Any help is appreciated.

S Atah Ahmed Khan
  • 1,313
  • 3
  • 14
  • 22
  • Possible duplicate of [Joining two table entities in Spring Data JPA](https://stackoverflow.com/questions/19977130/joining-two-table-entities-in-spring-data-jpa) – Shihe Zhang Aug 09 '18 at 06:37

1 Answers1

1

Its not the answer for joining via Hibernate, but alternatively you can create a view with your join and map the view to your objects

deterministicFail
  • 1,271
  • 9
  • 28
  • You map the view like a usual table, but you have to consider the object is readonly, and you need to compute a column with a unique value for the id. You didnt mention your DBMS, but you can make a composite of CacheMedia.id and ReleasDatetype.Id or you can add something like the ROWID (Oracle) Maybe this will help you further: http://stackoverflow.com/questions/15829848/to-map-a-database-view-with-no-primary-key-in-hibernate-xml-mapping – deterministicFail Nov 19 '13 at 10:52
  • DB dependency will not allow me to create a View.I need to look around and see what i can do. Do you have any other alternative? – S Atah Ahmed Khan Nov 20 '13 at 05:44
  • Have a look at this: http://stackoverflow.com/questions/17826080/hibernate-hql-selecting-objects-with-null-one-to-one-mapping-left-outer-join and on this: http://stackoverflow.com/questions/8784772/left-join-in-hibernate-one-to-zero-or-one-mapping – deterministicFail Nov 20 '13 at 08:05