当前页面: 开发资料首页 → J2EE 专题 → 请问hibernate如何映射双主键?在线等
请问hibernate如何映射双主键?在线等
摘要: 请问hibernate如何映射双主键?在线等
一个表有两个主键如:
create table safeIP(
IPgroup Number(10) ,
IP Number(10) ,
primary key(IPgroup ,IP)
)
这个时候如何写映射文件?
thanks!!!!!
.......................
.......................
这样就行,不过如果能改数据库的话,我还是建议LZ加一个Id字段最好。复合主键不好使的说。
看看我给你的代码,只要拷贝过去就可以用了
package model;
/**
* AbstractSafeip
*/
public abstract class AbstractSafeip implements java.io.Serializable {
// Fields
private SafeipId id;
// Constructors
/** default constructor */
public AbstractSafeip() {
}
/** full constructor */
public AbstractSafeip(SafeipId id) {
this.id = id;
}
// Property accessors
public SafeipId getId() {
return this.id;
}
public void setId(SafeipId id) {
this.id = id;
}
}
package model;
// Generated by MyEclipse - Hibernate Tools
/**
* Safeip
*/
public class Safeip extends AbstractSafeip implements java.io.Serializable {
// Constructors
/** default constructor */
public Safeip() {
}
/** full constructor */
public Safeip(SafeipId id) {
super(id);
}
}
package model;
/**
* SafeipId
*/
public class SafeipId implements java.io.Serializable {
// Fields
private long ipgroup;
private long ip;
// Constructors
/** default constructor */
public SafeipId() {
}
// Property accessors
public long getIpgroup() {
return this.ipgroup;
}
public void setIpgroup(long ipgroup) {
this.ipgroup = ipgroup;
}
public long getIp() {
return this.ip;
}
public void setIp(long ip) {
this.ip = ip;
}
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof SafeipId) ) return false;
SafeipId castOther = ( SafeipId ) other;
return (this.getIpgroup()==castOther.getIpgroup())
&& (this.getIp()==castOther.getIp());
}
public int hashCode() {
int result = 17;
result = 37 * result + (int) this.getIpgroup();
result = 37 * result + (int) this.getIp();
return result;
}
}
file name : Safeip.hbm.xml
<?xml version="1.0"?>
hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">