多对一单向关联

组和人员: 一个组有多个人员,一个人员属于一个组

在用户表里面有组的外键

Annota配置方式:

package com.zchen.hibernate.manytone;

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

@Entity
@Table(name="T_GROUP")
public class Group {
	private int id;
	private String name;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	

}

 

package com.zchen.hibernate.manytone;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
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;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@ManyToOne
	@JoinColumn(name="GROUP_ID")
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	

}

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory name="">
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql:///db_czbk_hibernate</property>
		<property name="connection.username">root</property>
		<property name="connection.password">1234</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hbm2ddl.auto">create</property>
		<property name="current_session_context_class">thread</property>
		<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
		<property name="show_sql">true</property>
		<mapping class="com.zchen.hibernate.manytone.Group" />
		<mapping class="com.zchen.hibernate.manytone.User" />
	</session-factory>
</hibernate-configuration>

 

package com.zchen.hibernate;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

public class Snippet {
	@Test
	public void testSchemaExport(){
			new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
	}
}

 

XML配置方式:

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zchen.hibernate.manytone">

    <class name="Group" table="T_GROUP">
        <id name="id" column="ID">
            <generator class="native">
            </generator>
        </id>
        <property name="name" column="NAME"/>
    </class>

</hibernate-mapping>

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zchen.hibernate.manytone">

    <class name="User" table="T_USER">
        <id name="id" column="ID">
            <generator class="native">
            </generator>
        </id>
        <property name="name" column="NAME"/>
        <many-to-one name="group" column="GROUP_ID"></many-to-one>
    </class>

</hibernate-mapping>

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory name="">
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql:///db_czbk_hibernate</property>
		<property name="connection.username">root</property>
		<property name="connection.password">1234</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hbm2ddl.auto">create</property>
		<property name="current_session_context_class">thread</property>
		<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
		<property name="show_sql">true</property>
		<mapping resource="com/zchen/hibernate/manytone/Group.hbm.xml" />
		<mapping resource="com/zchen/hibernate/manytone/User.hbm.xml" />
	</session-factory>
</hibernate-configuration>

 

package com.zchen.hibernate;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;

public class Snippet {
	@Test
	public void testSchemaExport(){
			new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
	}
}

 

你可能感兴趣的:(mysql,xml,Hibernate,jdbc,JUnit)