2009-10-30 152 views
2

我遇到了很大的困難,讓我的映射文件通過Hibernate中的外鍵與元素集合一起工作。 Java將嘗試在下面加載這些文件,並且不會有任何運行時異常,但事件表永遠不會作爲員工的對象加載(它將保持爲空,但從數據庫加載其他任何屬性)。在我的MySQL數據庫,我有以下幾點:幫助Hibernate集合外鍵映射

表:員工

主鍵:用戶名VARCHAR

名VARCHAR

AREACODE INTEGER

電話INTEGER

addressNumber INTEGER

addressLocation VARCHAR

電子郵件VARCHAR

表:活動

主鍵:ID INTEGER

的startDate的DateTime

結束日期的DateTime

客戶名稱VARCHAR

EM ployeeUsername 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。日期;

public class Event {private int ID; 私人日期startDate; 私人日期endDate; private String 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;

/** *此類表示員工的聯繫信息。 * */ 公共類聯繫{

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;

/** *此類表示公司數據庫中的員工。 * */ 公共類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; 
} 

}

回答

3

你的映射是錯的兩個(主)原因:

  1. 映射僱員和聯繫到同一個表的方式不會工作。 many-to-one需要一個外鍵(例如一個單獨的列);您正在嘗試重新使用主鍵。
  2. 將員工類重新映射到不同的表不會起作用 - 表和類不兼容。使用不同的實體名稱可以防止立即出現錯誤,但這不是適當的用法。

你應該做的卻是:

  1. 地圖Contactcomponent
  2. 直接在Employee上將事件映射爲one-to-many關聯。

喜歡的東西:

<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屬性以將該包排序)或將其映射爲composite elements的集合,其中每個事件將不再是一個獨立的實體。這是否適用取決於您的業務要求。

+0

非常感謝您的幫助。 – jds2501 2009-10-30 06:39:45