这是一个多对一单向关联的一个例子,目的是为了总结一下Hibernate的基础知识。
生成 Classes 和student类
package com.test.hibernate;
import java.util.Set;
public class Classes {
private int id;
private String name;
private Set students;
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;
}
public Set getStudents() {
return students;
}
public void setStudents(Set students) {
this.students = students;
}
}
package com.test.hibernate;
public class Student {
private int id;
private String name;
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;
}
}
生成Classes.hbm.xml和Student.hbm.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.test.hibernate">
<class name="Classes" table="t_classes">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="students">
<key column="classesid"/>
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
XML配置最主要的为:
<set name="students">
<key column="classesid"/>
<one-to-many class="Student"/>
</set>
将Student对应的表添加一个 classesid属性。
package com.test.hibernate;
public class Student {
private int id;
private String name;
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;
}
}
生成hibernate.cfg.xml文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost/mytest</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/test/hibernate/Student.hbm.xml"/>
<mapping resource="com/test/hibernate/Classes.hbm.xml"/>
</session-factory>
</hibernate-configuration>
写JAVA代码进行OR转换
为了避免重复写代码,可以把一些初始化的东西写到一个类中!其它类调用这个类的方法和属性。
HibernateUtils.java如下:
package com.test.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory factory;
static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}
生成主类 TestOne2Many.java
package com.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import com.test.hibernate.Classes;
import com.test.hibernate.HibernateUtils;
import com.test.hibernate.Student;
public class TestOne2Many {
public static void main(String[] args) {
testSave1();
}
public static void testSave1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student1 = new Student();
student1.setName("10");
session.save(student1);
Student student2 = new Student();
student2.setName("祖儿");
session.save(student2);
Set students = new HashSet();
students.add(student1);
students.add(student2);
Classes classes = new Classes();
classes.setName("尚学堂");
classes.setStudents(students);
session.save(classes);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
分享到:
相关推荐
这里包含了hibernate多对一单向关联关系实现源码,希望对你有用。
hibernate主键实现一对一单向关联关系源码
Hibernate 多对一外键单向关联 Hibernate 多对一连接表单向关联 Hibernate 多对多单向关联 Hibernate 一对一外键双向关联 Hibernate 一对一主键双向关联 Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向...
多对多单向关联 <br>需要注意映射规则: <set name="users" table="t_user_role"><br> <key column="roleid"/><br> <many-to-many class="com.bjsxt.hibernate.User" column="userid"/> </set>
hibernate关联映射注解多对多单向关联、
hibernate外键实现一对一单向关联关系源码
Hibernate 一对多 外键 单向关联,有例子。
Hibernate学习:单向多对一关联 工程
hibernate单向一对多关联映射(注解版)
包含《多对多双向关联映射》《多对一单向关联映射》《多对一双向关联映射》《一对多单向关联映射》等文档,并有图解及例子,非常适合新手学习,尤其是刚刚接触hibernate,对映射关系不清楚的。。。。
多对多双向关联 <br>注意映射规则: <set name="roles" table="t_user_role"><br> <key column="userid"/><br> <many-to-many class="com.bjsxt.hibernate.Role" column="roleid"/> </set><br> table...
Hibernate双向一对一关联映射(注解版)
NULL 博文链接:https://ganshisheng.iteye.com/blog/434420
│ ├─ 多对一外键单向关联 │ ├─ 多对一连接表单向关联 │ └─ 多对多单向关联 └─双向关联 ├─ 一对一外键双向关联 ├─ 一对一主键双向关联 ├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对...
博文链接:https://shaqiang32.iteye.com/blog/201308
博文链接:https://llying.iteye.com/blog/220809
博文链接:https://shaqiang32.iteye.com/blog/201314