Hibernate4.3.5 双向一对多示例

来源:互联网 发布:网络语言黑洞什么意思 编辑:程序博客网 时间:2024/06/05 12:07


源码下载地址





 

public class Group{

private long id;

private String name;

private Set<User> userSet=new HashSet<User>();;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set<User> getUserSet() {
return userSet;
}

public void setUserSet(Set<User> userSet) {
this.userSet = userSet;
}

public String toString(){
return "id:"+this.id+" \t name:"+this.name;
}


public class User{

private long id;

private String name;

private Group group;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Group getGroup() {
return group;
}

public void setGroup(Group group) {
this.group = group;
}

public String toString(){
return "id:"+this.id+"\tname:"+this.name;
}

} <?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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="entity.Group" table="zhtt0_group">
        <id name="id" type="long">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" not-null="true" />
        </property>
        <set name="userSet"
        table="zhtt0_user"
        lazy="false"
        cascade="save-update"
        inverse="false">
        <key column="groupId"/>
        <one-to-many class="entity.User"/>
        </set>
    </class>
</hibernate-mapping> <?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">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="entity.User" table="zhtt0_user">
        <id name="id" type="long">
            <column name="id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" not-null="true" />
        </property>
        <many-to-one name="group"
        class="entity.Group"
        column="groupId"
        cascade="save-update"
        lazy="false"/>
    </class>
</hibernate-mapping> public class UserTest01{
@Test
public void test01(){
Session session=HibernateUtil.getSessionFactory().openSession();
Transaction t=session.beginTransaction();
User u=new User();
u.setName("user"+(Math.random()+"").substring(0,10));
Group g=new Group();
g.setName("user"+(Math.random()+"").substring(0,10));
g.getUserSet().add(u);
session.save(g);
t.commit();
session.close();
}
} public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {


// 载入设置
Configuration configuration = new Configuration().configure();
// 创建serviceRegistry
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// 获取sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
System.err.println("InitialSessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}


public static SessionFactory getSessionFactory() {
return sessionFactory;
}


} <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC连接设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="hbm2ddl.auto">update</property>  


<!-- 连接池配置,使用c3p0连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>


<!-- 数据库语言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- sql设置 -->
<property name="show_sql">false</property>
<property name="format_sql">true</property>


<!-- 持久类设置所有需要持久的类要在这里标明,以下是针对annotation注释的类 -->
<mapping resource="entity/group.hbm.xml" />
<mapping resource="entity/user.hbm.xml" />
</session-factory>
</hibernate-configuration>
1 0
原创粉丝点击