`

Hibernate一对多映射

阅读更多
这只是我做的一个例子:

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>
分享到:
评论
2 楼 qsrock 2009-03-31  
存在的,ls的没看前面的sql语句吧!
1 楼 tvjody 2009-03-30  
<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>


你的vote_id在voteContect表中存在吗?

相关推荐

Global site tag (gtag.js) - Google Analytics