简体   繁体   中英

One object from two tables

I have two tables and want to map them to one object with Hibernate.

The reason for both tables is in the past and I wont change the frontend, which access data like this. I have Table Event (Event_ID, Preview, img) and Event_Details (ID, Event_ID, content).

I prepare one class in Java:

public class Event {
  private int event_ID;
  private String preview;
  private String img;
  private String content;
  //Getter and Setter
}

and following XML mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 <!-- Generated 16.03.2016 20:33:10 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="de.data.events.Event" table="ev1_event">
      <id name="id" type="int">
        <column name="Event_ID" />
        <generator class="assigned" />
      </id>
      <property name="preview" type="java.lang.String">
        <column name="ev1_preview" />
      </property>
      <property name="img" type="java.lang.String">
        <column name="ev1_img" />
      </property>
  </class>
  <class name="de.data.events.Event" table="pb1_event">
    <id name="id" type="int">
        <column name="id" />
        <generator class="assigned" />
    </id>
    //some properties
</class>

The part, where I have to join table1 to table2 is missing. But I didn´t found a way to fix my problem.

First, you'd have your Hibernate entities. Yours are Event and EventDetail , but for fun, let's go with Person and Address in this example. The key here is that you need a one-to-one or many-to-one relationship between your tables. Otherwise, your resultset will come out weird (more on that later).

@Entity
@Table(name = "PERSON")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PERSON_ID")
    public Integer id;

    @Column(name = "NAME")
    public String name;

    @Column(name = "HOME_ADDRESS_ID")
    public Integer homeAddressId;

    @Column(name = "TEMP_ADDRESS_ID")
    public Integer tempAddressId;

    // . . .  other fields,getters,setters

}

@Entity
@Table(name = "ADDRESS")
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ADDRESS_ID")
    public Integer id;

    @Column(name = "STREET")
    public String street;

    @Column(name = "CITY")
    public String city;

    // . . .  other fields,getters,setters

}

Then you'd have your target FlatObject POJO with the a constructor that will build it appropriately:

public class FlatObject {

    public final String name;
    public final String homeStreet;
    public final String homeCity;
    public final String tempStreet;
    public final String tempCity;

    public FlatEntity(String name, String homeStreet, String homeCity, String tempStreet, String tempCity) {
        this.name = name;
        this.homeStreet = homeStreet;
        this.homeCity = homeCity;
        this.tempStreet = tempStreet;
        this.tempCity = tempCity;
    }

    // . . .  other fields,getters

}

Finally, you'd leverage those objects with a Hibernate HQL SELECT where you join Person and Address and use their fields to construct a new FlatEntity :

SELECT new FlatEntity(p.name, ha.homeStreet, ha.homeCity, ta.tempStreet, ta.tempCity)
FROM
    Person p, Address ha, Address ta
WHERE
        p.homeAddressId = ha.id
    and p.tempAddressId = ta.id

As you can see, the HQL statment will join Person to Address twice: once for the home address and once for the temp address.

The same will hold true in your case. So in your case, if you're joining Event e, EventDetail ed WHERE e.id = ed.eventId , just be sure there's only one detail row per Event. Otherwise you'll get multiple rows when you have more than one details or you'll drop rows (because of the inner join) when an event has no details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM