两个表中的一个对象

时间:2016-05-11 19:34:39

标签: java sql hibernate

我有两个表,想要用Hibernate将它们映射到一个对象。

两个表的原因都在过去,我不会更改前端,这样可以访问这样的数据。 我有表事件(Event_ID,Preview,img)和Event_Details(ID,Event_ID,内容)。

我用Java编写了一个类:

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

并遵循XML映射文件:

<?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>

缺少必须将table1连接到table2的部分。但我找不到解决问题的方法。

1 个答案:

答案 0 :(得分:0)

首先,你有你的Hibernate实体。你的是EventEventDetail,但为了好玩,让我们在这个例子中使用PersonAddress。关键在于您需要表之间的一对一或多对一关系。否则,你的结果集会变得怪异(稍后会详细介绍)。

@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

}

然后你的目标FlatObject POJO会有一个构造函数来构建它:

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

}

最后,您将利用Hibernate HQL SELECT利用这些对象加入PersonAddress并使用其字段构建新的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

如您所见,HQL语句将Person加入Address两次:一次用于家庭地址,一次用于临时地址。

在你的情况下也是如此。因此,在您的情况下,如果您加入Event e, EventDetail ed WHERE e.id = ed.eventId,请确保每个事件只有一个详细信息行。否则,当您有多个详细信息时,您将获得多行,或者当事件没有详细信息时,您将删除行(因为内部联接)。