帮助Hibernate集合外键映射

我遇到了很大的困难,让我的映射文件通过Hibernate中的外键来处理元素的集合。 Java将尝试在下面加载这些文件,并且不会有任何运行时异常,但事件表永远不会作为员工的对象加载(它将保持为空,但从数据库加载其他任何属性)。 在我的MySQL数据库中,我有以下内容:

表:员工

主键:username varchar

名称varchar

areacode INTEGER

电话INTEGER

地址号INTEGER

addressLocation varchar

email varchar

表格:事件

主键:ID INTEGER

startDate DateTime

endDate DateTime

customerName varchar

employeeUsername varchar

<?xml version="1.0"?>

<class name="Employee" table="Employees">
    <id name="username" column="username" type="string"/>
    <many-to-one name="contact" class="Contact" column="username" unique="false" update="false" insert="false" optimistic-lock="true" not-found="exception" embed-xml="true" />
</class>

<class name="Contact" table="Employees">
    <id name="username" column="username" type="string"/>
    <property name="name" column="name" type="string"/>
    <property name="addressNumber" column="addressNumber" type="int"/>
    <property name="areacode" column="areacode" type="int"/>
    <property name="phone" column="phone" type="int"/>
    <property name="addressLocation" column="addressLocation" type="string"/>
    <property name="email" column="email" type="string"/>
</class>

<?xml version="1.0"?>

<class name="Event" table="Events">
    <id name="ID" column="ID" type="int">
        <generator class="assigned"/>
    </id>
    <property name="startDate" column="startDate" type="date"/>
    <property name="endDate" column="endDate" type="date"/>
    <property name="customerName" column="customerName" type="string"/>
</class>

<class name="Employee" table="Events" entity-name="Employee2">
    <id name="username" column="employeeUsername" type="string"/>
    <list name="events" cascade="all">
        <key column="employeeUsername"/>
        <list-index column="ID"/>
        <one-to-many class="Event"/>
    </list>
</class>

假设hibernate.cfg.xml文件存在并且可行(如果您觉得需要显示,请告诉我)。 它确实在其映射文件部分包含了这两个文件。

我有一种感觉,我的错误可能出现在我的“list”声明中的事件集合声明中。 这里是Java类:

package org.hibernate.employee;

import java.util.Date;

公共类Event {private int ID; 私人日期startDate; 私人日期结束日期; 私人字符串customerName;

public Event(){
    System.out.println("blah");
}

/**
 * @return the iD
 */
public int getID() {
    return ID;
}
/**
 * @param id the iD to set
 */
public void setID(int id) {
    ID = id;
}
/**
 * @return the startDate
 */
public Date getStartDate() {
    return startDate;
}
/**
 * @param startDate the startDate to set
 */
public void setStartDate(Date startDate) {
    this.startDate = startDate;
}
/**
 * @return the endDate
 */
public Date getEndDate() {
    return endDate;
}
/**
 * @param endDate the endDate to set
 */
public void setEndDate(Date endDate) {
    this.endDate = endDate;
}
/**
 * @return the customerName
 */
public String getCustomerName() {
    return customerName;
}
/**
 * @param customerName the customerName to set
 */
public void setCustomerName(String customerName) {
    this.customerName = customerName;
}

}

/*

*文件:Contact.java * /

包org.hibernate.employee;

/ ** *此类表示员工的联系信息。 * * / public class Contact {

private int username; // the contact identifier in the database
private int areacode; // the contact areacode
private int phone; // the contact phone
private String name; // the contact's name
private int addressNumber; // the contact's address number
private String addressLocation; // the contact's address location
private String email; // the contact's email

/**
 * Constructs a Contact object.
 */
public Contact() {

}

/**
 * @return the areacode
 */
public int getAreacode() {
    return areacode;
}

/**
 * @param areacode the areacode to set
 */
public void setAreacode(int areacode) {
    this.areacode = areacode;
}

/**
 * @return the phone
 */
public int getPhone() {
    return phone;
}

/**
 * @param phone the phone to set
 */
public void setPhone(int phone) {
    this.phone = phone;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

/**
 * @return the addressNumber
 */
public int getAddressNumber() {
    return addressNumber;
}

/**
 * @param addressNumber the addressNumber to set
 */
public void setAddressNumber(int addressNumber) {
    this.addressNumber = addressNumber;
}

/**
 * @return the addressLocation
 */
public String getAddressLocation() {
    return addressLocation;
}

/**
 * @param addressLocation the addressLocation to set
 */
public void setAddressLocation(String addressLocation) {
    this.addressLocation = addressLocation;
}

/**
 * @return the email
 */
public String getEmail() {
    return email;
}

/**
 * @param email the email to set
 */
public void setEmail(String email) {
    this.email = email;
}

public String toString(){
    String retVal = "";
    retVal += "Address: " + this.addressNumber + " " + this.addressLocation + "n";
    retVal += "Email: " + this.email + "n";
    retVal += "Phone: " + this.areacode + " " + this.phone + "n";
    retVal += "Name: " + this.name + "n";
    return retVal;
}

public void setUsername(int username) {
    this.username = username;
}

public int getUsername() {
    return username;
}

}

/*

*文件:Employee.java * /

包org.hibernate.employee; import java.util.List;

/ ** *此类表示公司数据库中的员工。 * * / public class Employee {

private String username; // the employee's username
private Contact contact; // the employee's contact information
private List events;

/**
 * Constructs an Employee object.
 */
public Employee() {
    super();
}

/**
 * @return the username
 */
public String getUsername() {
    return username;
}

/**
 * @param username the username to set
 */
public void setUsername(String username) {
    this.username = username;
}

/**
 * @return the contact
 */
public Contact getContact() {
    return contact;
}

/**
 * @param contact the contact to set
 */
public void setContact(Contact contact) {
    this.contact = contact;
}

/**
 * @return the events
 */
public List getEvents() {
    return events;
}

/**
 * @param events the events to set
 */
public void setEvents(List events) {
    this.events = events;
}

public String toString(){
    String retVal = "";
    System.out.println(events);
    retVal += "Username: " + username + "n";
    retVal += "Contact: " + contact + "n";
    return retVal;
}

}


您的映射出于两个(主要)原因是错误的:

  • 将员工和联系人映射到同一张表是不行的。 many-to-one需要一个外键(例如一个单独的列); 您正在尝试重新使用主键。
  • 将Employee类重新映射到不同的表不会起作用 - table和class不兼容。 使用不同的实体名称可以防止立即出现错误,但这不是适当的用法。
  • 你应该做的是:

  • Map Contact作为组件。
  • 直接在Employee上将事件集合映射为一对多关联。
  • 就像是:

    <class name="Employee" table="Employees">
      <id name="username" column="username" type="string"/>
    
      <component name="Contact" class="Contact"> <!-- class attribute optional -->
        <property name="name" column="name" type="string"/>
        <property name="addressNumber" column="addressNumber" type="int"/>
        <property name="areacode" column="areacode" type="int"/>
        <property name="phone" column="phone" type="int"/>
        <property name="addressLocation" column="addressLocation" type="string"/>
        <property name="email" column="email" type="string"/>
      </component>
      <list name="events" cascade="all">
        <key column="employeeUsername"/>
        <list-index column="event_idx"/>
        <one-to-many class="Event"/>
      </list>  
    </class>
    

    请注意,如果要将事件列表映射为有序列表,则list-index应该真的是表中的单独列; 你不能将它映射到id。 如果不希望把它定义为一个柱,既可以映射列表作为<bag>代替(和,可选地,指定order-by属性进行排序该袋通过)或它映射为复合元件的集合,其中每个事件将不再是一个独立的实体。 这是否适用取决于您的业务要求。

    链接地址: http://www.djcxy.com/p/68665.html

    上一篇: Help with Hibernate Collection Foreign Key Mapping

    下一篇: easiest way to find missing css classes and ids