这只是我做的一个例子:
sql语句:
create table vote(
id int auto_increment primary key not null,
creator varchar(20) not null,
title varchar(50) not null,
startTime date not null,
endTime date not null
);
create table voteContect(
id int auto_increment primary key not null,
thing varchar(100) not null,
vote_id int ,
agree int default 0,
disagree int default 0
);
alter table voteContect add constraint belongs_to foreign key (vote_id) references vote(id);
java代码:
/**
* Vote.java(大部分由工具生成的)
*/
package com.huanglq.pojo;
import java.sql.Date;
import java.util.Set;
import com.huanglq.util.DateTime;
public class Vote implements java.io.Serializable {
// Fields
private Integer id;
private String creator;
private String title;
private Date startTime;
private Date endTime;
/**投票的事件*/
private Set<VoteContect> voteContects;
// Constructors
public Set<VoteContect> getVoteContects() {
return voteContects;
}
public void setVoteContects(Set<VoteContect> voteContects) {
this.voteContects = voteContects;
}
/** default constructor */
public Vote() {
}
/** minimal constructor */
public Vote(String title, Date startTime, Date endTime) {
this.title = title;
this.startTime = startTime;
this.endTime = endTime;
}
/** full constructor */
public Vote(String creator, String title, Date startTime, Date endTime) {
this.creator = creator;
this.title = title;
this.startTime = startTime;
this.endTime = endTime;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCreator() {
return this.creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getStartTime() {
return this.startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return this.endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public static void main(String [] args){
Vote vote=new Vote();
vote.setTitle("Welcome to here");
vote.setCreator("kyo");
System.out.println("2007-12-7");
vote.setStartTime(new java.sql.Date(DateTime.toUtilDate("2007-12-7").getTime()));
vote.setEndTime(new java.sql.Date(new java.util.Date().getTime()));
System.out.println(vote.getTitle());
System.out.println(vote.getStartTime());
System.out.println(vote.getEndTime());
}
}
/**
* VoteContect.java(大部分由工具生成的)
*/
package com.huanglq.pojo;
public class VoteContect implements java.io.Serializable {
// Fields
private Integer id;
private String thing;
/**投票*/
private Vote vote;
private Integer agree;
private Integer disagree;
// Constructors
/** default constructor */
public VoteContect() {
}
/** minimal constructor */
public VoteContect(String thing) {
this.thing = thing;
}
/** full constructor */
public VoteContect(String thing, Integer agree,
Integer disagree) {
this.thing = thing;
this.agree = agree;
this.disagree = disagree;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getThing() {
return this.thing;
}
public void setThing(String thing) {
this.thing = thing;
}
public Integer getAgree() {
return this.agree;
}
public void setAgree(Integer agree) {
this.agree = agree;
}
public Integer getDisagree() {
return this.disagree;
}
public void setDisagree(Integer disagree) {
this.disagree = disagree;
}
public Vote getVote() {
return vote;
}
public void setVote(Vote vote) {
this.vote = vote;
}
}
映射文件代码:
Vote.hbm.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.huanglq.pojo.Vote" table="vote" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="creator" type="java.lang.String">
<column name="creator" length="20" />
</property>
<property name="title" type="java.lang.String">
<column name="title" length="50" />
</property>
<property name="startTime" type="java.sql.Date">
<column name="startTime" length="10" />
</property>
<property name="endTime" type="java.sql.Date">
<column name="endTime" length="10"/>
</property>
<set name="voteContects" lazy="true" inverse="true" cascade="all" >
<key>
<!-- vote_id是voteContect表的字段,而不是vote表的 -->
<column name="vote_id"/>
</key>
<one-to-many class="com.huanglq.pojo.VoteContect" />
</set>
</class>
</hibernate-mapping>
VoteContect.hbm.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.huanglq.pojo.VoteContect" table="voteContect" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="thing" type="java.lang.String">
<column name="thing" length="100" />
</property>
<many-to-one name="vote" class="com.huanglq.pojo.Vote" cascade="none"
outer-join="auto" update="true" insert="true" access="property"
column="vote_id" not-null="true"/>
<property name="agree" type="java.lang.Integer">
<column name="agree" />
</property>
<property name="disagree" type="java.lang.Integer">
<column name="disagree" />
</property>
</class>
</hibernate-mapping>
分享到:
相关推荐
hibernate一对多映射实例,包换数据库建表代码
Hibernate一对多映射配置详解,很值得借鉴
hibernate的简单搭建、hibernate的一对多映射关系
这个例子会让你很快懂得hibernate一对多的映射机制,你看了以后会有很大的感触!
hibernate一对多映射源代码 希望大家可以顶一下。。
一个老师可以教多个学生 【一对多映射】 多个学生可以被一个老师教【多对一映射】 部门与员工: 一个部门有多个员工【一对多映射】 多个员工属于一个部门【多对一映射】 1.2:多对多,举例说明: 项目和开发员工:...
hibernate基于 一对多、多对一映射写的一个增加,查询,删除,修改的demo,内附数据库脚本
Hibernate一对多,多对一映射
hibernate 多对一映射 适合初学者
hibernate核心,一对多,多对多映射讲解,看了就完全搞明白了
hibernate多对一映射(XML)版
Hibernate多对一映射(注解版)
包含《多对多双向关联映射》《多对一单向关联映射》《多对一双向关联映射》《一对多单向关联映射》等文档,并有图解及例子,非常适合新手学习,尤其是刚刚接触hibernate,对映射关系不清楚的。。。。
Hibernate多对一映射实例,里面包含源程序,测试程序,映射说明
Hibernate双向一对一关联映射(注解版)
Hibernate映射一对多关联关系