`
laodaobazi
  • 浏览: 272785 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate 使用 Annotation 4(多对一单向关联)

阅读更多

Group.java代码:

package com.jlee04.money2onedanxiang;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="T_GROUP")
public class Group {

	private int id ;
	private String name ;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name="name" , length=32)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

 

 

User.java代码:

package com.jlee04.money2onedanxiang;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="T_USER")
public class User {

	private int id ;
	private String name ;
	private Group group ;
	
	@ManyToOne(fetch=FetchType.LAZY , cascade={CascadeType.ALL} )
	@JoinColumn(name="groupId")
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name="name" , length=32)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

 

 

RelationsTest.java代码:

package com.jlee04.money2onedanxiang;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class RelationsTest extends TestCase {

	public void testRelation(){
		SessionFactory sf = new AnnotationConfiguration()
														.configure().buildSessionFactory();
		Session session = sf.getCurrentSession() ;
		session.getTransaction().begin() ;
		
		
		
		session.getTransaction().commit() ;
	}
}

 

hibernate.cfg.xml配置文件:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">100</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
		
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <property name="format_sql">true</property>
		
        <!-- Drop and re-create the database schema on startup -->
        <!--<property name="hbm2ddl.auto">create</property>-->

		<mapping class="com.jlee04.money2onedanxiang.Group"/>
		<mapping class="com.jlee04.money2onedanxiangUser"/>

    </session-factory>

</hibernate-configuration>

 

分享到:
评论

相关推荐

    Hibernate_Annotation关联映射

    使用@ManyToOne批注来实现多对一关联。 @ManyToOne批注有一个名为targetEntity的参数,该参数定义了目标实体名,通常不需要定义该参数,因为在大部分情况下默认值(表示关联关系的属性类型)就可以很好的满足需求了。...

    Hibernate Annotation 基于连接表的单向一对多关联

    NULL 博文链接:https://paladin1988.iteye.com/blog/1634669

    Hibernate多对一单向关联(annotation/xml)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1932861

    Hibernate Annotation 基于外键的单向多对一关联

    NULL 博文链接:https://paladin1988.iteye.com/blog/1633417

    Hibernate多对多单向关联(annotation/xml)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1932868

    Hibernate一对多单向关联(annotation/xml)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1932864

    Hibernate一对一单向外键关联 (联合主键annotation)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1932507

    Hibernate一对一单向外键关联(annotation/xml)

    NULL 博文链接:https://cdxs2.iteye.com/blog/1930748

    精通Java Web整合开发(第2版)

    12.4.5 多对一单向关联的annotation注解实现538 12.4.6 一对多双向关联的annotation注解实现540 12.4.7 一对多双向自身关联的annotation注解实现542 12.4.8 多对多单向关联的annotation注解实现543 12.4.9 多对多...

    Hibernate+中文文档

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...

    hibernate3.2中文文档(chm格式)

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...

    HibernateAPI中文版.chm

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...

    Hibernate中文详细学习文档

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...

    最全Hibernate 参考文档

    7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to ...

    Hibernate 中文 html 帮助文档

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...

    Hibernate教程

    8.4.1. 一对多(one to many) / 多对一(many to one) 8.4.2. 一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many ...

    hibernate 体系结构与配置 参考文档(html)

    一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...

    Hibernate3的帮助文档

    8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖...

    Hibernate注释大全收藏

    这种策略支持双向的一对多关联,但不支持 IDENTIFY 生成器策略,因为ID必须在多个表间共享。一旦使用就不能使用AUTO和IDENTIFY生成器。 每个类层次结构一张表 @Entity @Inheritance(strategy=InheritanceType....

Global site tag (gtag.js) - Google Analytics