浏览 9560 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2004-08-04
hbm
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" > <hibernate-mapping> <class name="com.dsii.hibernate.po.UserRole" table="role_user" > <composite-id class="com.dsii.hibernate.po.UserRoleID" name="userRoleID"> <key-property name="userNo" column="user_id" type="string"/> <key-property name="roleNo" column="role_id" type="string"/> </composite-id> <property name="notes" column="notes"/> </class> </hibernate-mapping> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2004-08-04
ID
package com.dsii.hibernate.po; import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * <p>Title:UserRoleID.java</p> * <p>Description:</p> * <p>Copyright:Copyright (c); 2004 DSII,Inc</p> * <p>Company:DSII,Inc</p> * @author Jason 2004-8-3 * @version 1.0 */ public class UserRoleID implements Serializable { private String userNo; private String roleNo; public String getRoleNo(); { return roleNo; } public String getUserNo(); { return userNo; } public void setRoleNo(String string); { roleNo = string; } public void setUserNo(String string); { userNo = string; } public boolean equals(Object other); { if (!(other instanceof UserRoleID);); return false; UserRoleID castOther = (UserRoleID); other; return new EqualsBuilder(); .append(this, castOther); .append(userNo, castOther.userNo); .append(roleNo, castOther.roleNo); .isEquals();; } public int hashCode(); { return new HashCodeBuilder();.append(userNo);.append(roleNo);.toHashCode();; } } |
|
返回顶楼 | |
发表时间:2004-08-04
PO
package com.dsii.hibernate.po; /** * <p>Title:UserRole.java</p> * <p>Description:</p> * <p>Copyright:Copyright (c); 2004 DSII,Inc</p> * <p>Company:DSII,Inc</p> * @author Jason 2004-8-3 * @version 1.0 */ public class UserRole { private UserRoleID userRoleID; private String notes; public String getNotes(); { return notes; } public UserRoleID getUserRoleID(); { return userRoleID; } public void setNotes(String string); { notes = string; } public void setUserRoleID(UserRoleID roleID); { userRoleID = roleID; } } |
|
返回顶楼 | |
发表时间:2004-08-04
main
package com.dsii.hibernate.po; import net.sf.hibernate.HibernateException; import com.dsii.common.hibernate.HibernateUtil; /** * <p>Title:UserRoleTest.java</p> * <p>Description:</p> * <p>Copyright:Copyright (c); 2004 DSII,Inc</p> * <p>Company:DSII,Inc</p> * @author Jason 2004-8-3 * @version 1.0 */ public class UserRoleTest { public static void main(String[] args); { UserRoleID userRoleID = new UserRoleID();; userRoleID.setUserNo("u001");; userRoleID.setRoleNo("r001");; UserRole userRole = new UserRole();; userRole.setUserRoleID(userRoleID);; userRole.setNotes("notes");; try { HibernateUtil.add(userRole);; } catch (HibernateException e); { e.printStackTrace();; } try { userRoleID = new UserRoleID();; userRoleID.setUserNo("u001");; userRoleID.setRoleNo("r001");; userRole = (UserRole); HibernateUtil.findById( UserRole.class,userRoleID);; System.out.println("..............:" + userRole.getNotes(););; } catch (HibernateException e); { e.printStackTrace();; } } } |
|
返回顶楼 | |
发表时间:2004-08-04
DDL
Table Name:role_user Column Name:role_id,user_id,notes,都为字符串类型的就OK了 Good Luck. |
|
返回顶楼 | |
发表时间:2004-08-04
对多表关连而言的composite-id使用方法
只要将hbm中的composite-id改为 <composite-id class="com.dsii.hibernate.po.UserRoleID" name="userRoleID" > <key-many-to-one name="user" class="com.dsii.hibernate.po.User" column="user_id"/> <key-many-to-one name="role" class="com.dsii.hibernate.po.Role" column="role_id"/> </composite-id> 再把UserRoleID.java中的两个field改为User,Role型的就可以了 Good Luck. |
|
返回顶楼 | |
发表时间:2004-08-04
谢谢!
请问 HibernateUtil.findById(); HibernateUtil.add();是什么作用?add是save()吗?findById()呢?排序吗? |
|
返回顶楼 | |
发表时间:2004-08-05
HibernateUtil.add()其实就是Session.save(Object obj);
HibernateUtil.findById()其实就是Session.load(Class clazz,Serializable pk) ; 这种单笔查询就不用排序了吧 |
|
返回顶楼 | |
发表时间:2004-10-10
楼上的,问一个问题,你提出的例子只说明了如何进行保存(insert/update)和查询(select),那么如何进行删除呢,尤其是,当复合主键的一个字段是另外一个表的外键时,应该怎么处理呢?
|
|
返回顶楼 | |