如何在hibernate xml中映射带有关联的java.util.Map?

时间:2014-06-14 06:51:13

标签: java hibernate jpa orm hibernate-mapping

我有实体Person如下:

@Entity
@Table(name = "Person")
@Inheritance(strategy = InheritanceType.JOINED)
public class Person implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "personName", length = 16, nullable = false)
    private String name;
.....................
}

我有实体Teacher如下:

@Entity
@Table(name = "teacher")
@PrimaryKeyJoinColumn(name = "PersonId")
public class Teacher extends Person implements Serializable {

    private Map<String, Child> childByName = new HashMap<>();
        ......................................
}

我有实体Child如下:

@Entity
@Table(name = "CHILD")
@PrimaryKeyJoinColumn(name = "PersonId")
public class Child extends Person implements Serializable {
        ..............................
}

地图Teacher.childByName应将name实体的Child属性作为关键字,将Child实体映射为值。 TeacherChild之间的关系是一对多的。

我需要这个映射是xml格式。 现在我在Person.hbm.xml中执行了此操作:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.masterhibernate.SimpleHibernateDemo.Person"
        table="Person">
        <cache usage="read-write" />

        <id name="id" column="id">
            <generator class="native" />
        </id>

        <property name="name">
            <column name="name" length="24" not-null="true" />
        </property>
        <property name="surname">
            <column name="surname" length="36"></column>
        </property>
        <property name="address">
            <column name="address" length="32"></column>
        </property>
        <joined-subclass name="com.masterhibernate.SimpleHibernateDemo.Parent"
            table="Parent">
            <key column="person_id" foreign-key="parent_person" />
            <property name="job" column="WorkPlace" length="22" type="string" />
            <set name="children" inverse="true" cascade="save-update" lazy="true">
                <cache usage="read-write" />
                <!-- specifies foreign key column of child table -->
                <key column="ParentPK" />
                <one-to-many class="com.masterhibernate.SimpleHibernateDemo.Child" />
            </set>
        </joined-subclass>
        <joined-subclass name="com.masterhibernate.SimpleHibernateDemo.Child"
            table="Child">
            <key column="person_id" foreign-key="child_person" />
            <property name="toy" column="toy" length="55" type="string" />
            <many-to-one name="parent"
                class="com.masterhibernate.SimpleHibernateDemo.Parent" column="ParentPK"
                lazy="false" fetch="join" foreign-key="child_parent" />
            <set name="teachers" table="TeacherPupil" inverse="true" lazy="true">
                <key column="child_id" foreign-key="teacherPupil_child" />
                <many-to-many class="com.masterhibernate.SimpleHibernateDemo.Teacher"
                    column="teacher_id" />
            </set>
        </joined-subclass>
        <joined-subclass name="com.masterhibernate.SimpleHibernateDemo.Teacher"
            table="Teacher">
            <key column="person_id" foreign-key="teacher_person" />
            <property name="subject" column="subject" length="25" type="string" />
            <set name="children" table="TeacherPupil" lazy="false" fetch="join">
                <cache usage="read-write" />
                <key column="teacher_id" foreign-key="teacherPupil_teacher" />
                <many-to-many class="com.masterhibernate.SimpleHibernateDemo.Child"
                    column="child_id" />
            </set>
            <map name="childByName" table="Child_By_Name" embed-xml="true">
                <key column="childByName_id" />
                <index column="childName" type="string" />
                <one-to-many class="com.masterhibernate.SimpleHibernateDemo.Child" />
            </map>
        </joined-subclass>
    </class>
</hibernate-mapping>

不幸的是<map>甚至没有创建名为Child_By_Name的单独表格。相反,它会在表格childByName中创建列childNameChild。这很奇怪。

那么,如何将我的childByName地图映射到单独的表中以引用Child作为其值?

2 个答案:

答案 0 :(得分:2)

我很欣赏所有答案,但你能做的另一件事是, 建立一个实体:

@Entity
@XmlRootElement(name = "ChildName")
@Table(name="ChildName")
public class ChildName{
@Id
private Long  id;
private String name;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "childId")
private Child child;

}

并将您的专栏加入

@Entity
@XmlRootElement(name = "Teacher")
@Table(name="Teacher")
public class Teacher {

@Id
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "childNameId")
private ChildName childName;

}

您的整个代码将

@Entity
@XmlRootElement(name = "Teacher")
@Table(name="Teacher")
public class Teacher {

@Id
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "childNameId")
private ChildName childName;

}

@Entity
@XmlRootElement(name = "ChildName")
@Table(name="ChildName")
class ChildName{
@Id
private Long  id;
private String name;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "childId")
private Child child;

}

@Entity
@XmlRootElement(name = "Child")
@Table(name="Child")
public class Child extends Person implements Serializable {

}

@Entity
@XmlRootElement(name = "Person")
@Table(name="Person")
public class Person implements Serializable {
@Id
private Long id;
private String name;
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

它不会像那样工作的地图协会

答案 1 :(得分:1)

由于您已经有了一个TeacherPupil表,因此您也应该将它用于Map关联:

<map name="childByName" table="TeacherPupil" inverse="true">

<key column="teacher_id" not-null="true"/>

<map-key-many-to-many column="name" class="Child"/>

<many-to-many class="Child"/>

</map>