設置了許多在休眠一對多的關係,我有以下情況:如何使用XML映射
我新的休眠和我有一個項目將在未來幾天完成。
這是關於Java.The第一步驟的CRUD Web應用程序已經完成,但我很堅持,我不能找到在互聯網有關下列情況的任何幫助:
我有一個項目表,可以有動作表中的許多動作。 (多對多關係)。
我也有具有主鍵的付款表,(paymentId
)和項目的2個外鍵和動作(專案編號,actionId)以及其他一些領域,如paymentMethod
,price
,startDate
,endDate
。我實際上使用付款表來連接每個項目,以添加一些額外的信息,如金額等每個項目。
我希望我澄清一下我的想法。
我不知道我必須如何做映射文件?我必須創建2個映射文件還是3個? (Project.hbm.xml
,action.hbm.xml
和payments.hbm.xml
)
一開始我還以爲做分裂關係到以下幾點:
項目POJO類有一個與支付一對多的關係(和XML映射將在如與一對多關係爲1),動作POJO類與付款(也與相關的XML映射文件)具有多對一關係。
還有一個POJO類的付款方式,包括Objects Action和Projects,以及XML文件中相關的一對多關係。
我還沒有看到這樣的實施任何教程,既不是任何網站,我不知道如果這是好的?
我唯一發現的是多對多的關係使用註釋(主要)和中間步驟表(在我的情況下支付)只有2個外鍵沒有任何主鍵和額外的領域,像這些我想要。
項目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.nisid.entities.Project" table="projects">
<meta attribute="class-description"> This class contains the project records. </meta>
<id name="projectId" type="int" column="projectId">
<generator class="native">
</generator>
</id>
<many-to-one name="fkCustomer" class="com.nisid.entities.Customers"
fetch="select">
<column name="customerId" not-null="true"/>
</many-to-one>
<set name="payments"
lazy="true" fetch="select" cascade="all">
<key>
<column name="projectId" />
</key>
<one-to-many class="com.nisid.entities.Payment" />
</set>
<property name="projectName" column="projectName" type="string"/>
<property name="projectDescription" column="description" type="string"/>
</class>
</hibernate-mapping>
動作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.nisid.entities.Action" table="actions" >
<id name="actionId" type="int">
<column name="actionId" />
<generator class="native" >
</generator>
</id>
<property name="actionName" type="string">
<column name="actionName" />
</property>
<set name="payments" inverse="true" lazy="true" fetch="select">
<key>
<column name="actionId" />
</key>
<one-to-many class="com.nisid.entities.Payment" />
</set>
</class>
</hibernate-mapping>
付款(XML映射):
<!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.nisid.entities.Payment" table="payments">
<composite-id name="paymentId" class="com.nisid.entities.PaymentID" >
<key-property name="myproject" column="projectId" />
<key-property name="myaction" column="actionId" />
</composite-id>
<component name="myproject">
<many-to-one name="project" class="com.nisid.entities.Project"
>
<column name="projectId" not-null="true" />
</many-to-one>
</component>
<component name="myaction">
<many-to-one name="action" class="com.nisid.entities.Action"
>
<column name="actionId" not-null="true" />
</many-to-one>
</component>
<property name="amount" column="amount" type="int"/>
<property name="paymentDate" column="paymentDate" type="date"/>
<property name="paymentExpire" column="paymentExpire" type="date"/>
<property name="paymentMethod" column="paymentMethod" type="string"/>
</class>
而且ppojo類:
行動:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.nisid.entities;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
*
* @author p293
*/
public class Action {
private int actionId;
private String actionName;
private Set payments=new HashSet();
public Action(){}
public Action(String actionName) {
this.actionName = actionName;
}
public int getActionId() {
return actionId;
}
public void setActionId(int actionId) {
this.actionId = actionId;
}
public String getActionName() {
return actionName;
}
public void setActionName(String actionName) {
this.actionName = actionName;
}
public Set getPayments() {
return payments;
}
public void setPayments(Set payments) {
this.payments = payments;
}
@Override
public int hashCode() {
int hash = 5;
hash = 89 * hash + Objects.hashCode(this.actionName);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Action other = (Action) obj;
if (!Objects.equals(this.actionName, other.actionName)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Action{" + "=" + actionName + '}';
}
}
項目:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.nisid.entities;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
*
* @author p293
*/
public class Project {
private int projectId;
private String projectName;
private String projectDescription;
private Customers fkCustomer;
private Set payments=new HashSet();
public Project(){
}
public Project(String projectName, String projectDescription) {
this.projectName = projectName;
this.projectDescription = projectDescription;
}
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getProjectDescription() {
return projectDescription;
}
public void setProjectDescription(String projectDescription) {
this.projectDescription = projectDescription;
}
public Customers getFkCustomer() {
return fkCustomer;
}
public void setFkCustomer(Customers fkCustomer) {
this.fkCustomer = fkCustomer;
}
public Set getPayments() {
return payments;
}
public void setPayments(Set payments) {
this.payments = payments;
}
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + Objects.hashCode(this.projectName);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Project other = (Project) obj;
if (!Objects.equals(this.projectName, other.projectName)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Project{" + "projectName=" + projectName + ",with description=" + projectDescription + '}';
}
}
付款方式:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.nisid.entities;
import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
*
* @author p293
*/
public class Payment {
private PaymentID paymentId=new PaymentID();
private int amount;
private Date paymentDate;
private Date paymentExpire;
private String paymentMethod;
public Payment(int fkProjectId, int fkActionId, int amount, Date paymentDate, Date paymentExpire, String paymentMethod) {
this.amount = amount;
this.paymentDate = paymentDate;
this.paymentExpire = paymentExpire;
this.paymentMethod=paymentMethod;
}
public Payment(){}
public PaymentID getPaymentId() {
return paymentId;
}
public void setPaymentId(PaymentID paymentId) {
this.paymentId = paymentId;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public Date getPaymentDate() {
return paymentDate;
}
public void setPaymentDate(Date paymentDate) {
this.paymentDate = paymentDate;
}
public Date getPaymentExpire() {
return paymentExpire;
}
public void setPaymentExpire(Date paymentExpire) {
this.paymentExpire = paymentExpire;
}
public String getPaymentMethod() {
return paymentMethod;
}
public void setPaymentMethod(String paymentMethod) {
this.paymentMethod = paymentMethod;
}
public Project getProject(){
return getPaymentId().getProject();
}
public Action getAction(){
return getPaymentId().getAction();
}
public void setAction(Action action){
getPaymentId().setAction(action);
}
public void setProject(Project project){
getPaymentId().setProject(project);
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + this.amount;
hash = 89 * hash + Objects.hashCode(this.paymentMethod);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Payment other = (Payment) obj;
if (this.amount != other.amount) {
return false;
}
if (!Objects.equals(this.paymentMethod, other.paymentMethod)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Payment{" + "amount=" + amount + ", paymentMethod=" + paymentMethod + '}';
}
}
PaymentId:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.nisid.entities;
import java.util.Objects;
/**
*
* @author lurtzaki
*/
public class PaymentID {
private Project myproject;
private Action myaction ;
public PaymentID(){
super();}
public Project getProject() {
return myproject;
}
public void setProject(Project project) {
this.myproject = project;
}
public Action getAction() {
return myaction;
}
public void setAction(Action action) {
this.myaction = action;
}
@Override
public int hashCode() {
int hash = 3;
hash = 17 * hash + Objects.hashCode(this.myproject);
hash = 17 * hash + Objects.hashCode(this.myaction);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final PaymentID other = (PaymentID) obj;
if (!Objects.equals(this.myproject, other.myproject)) {
return false;
}
if (!Objects.equals(this.myaction, other.myaction)) {
return false;
}
return true;
}
}
新的XML:
<composite-id name="paymentId" class="com.nisid.entities.PaymentID">
<key-many-to-one name="myproject" class="com.nisid.entities.Project">
<column name="projectId"/>
</key-many-to-one>
<key-many-to-one name="myaction" class="com.nisid.entities.Action">
<column name="actionId" />
</key-many-to-one>
</composite-id>
<property name="amount" column="amount" type="int"/>
<property name="paymentDate" column="paymentDate" type="date"/>
<property name="paymentExpire" column="paymentExpire" type="date"/>
<property name="paymentMethod" column="paymentMethod" type="string"/>
可以有很多行動的項目表不會很多,除非這些行動也可以有很多項目。對此的澄清可以幫助某人更好地回答這個問題。 http://www.mkyong.com/hibernate/hibernate-many-to-many-relationship-example/ – jmpyle771
是的,這是真的。行動也可以有很多項目..所以多對多是雙向關係是我的情況! –