(八)Hibernate 映射关系
所有项目导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java
第一节:Hibernate 一对一映射关系实现
1,按照主键映射;
2,按照外键映射;
1,按照主键映射:
User.java
package com.wishwzp.model; public class User { private int id;
private String name;
private Address address; 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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
} }
Address.java
package com.wishwzp.model; public class Address { private int id;
private String address;
private String zipcode;
private User user; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
} }
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wishwzp.model"> <class name="User" table="t_user">
<id name="id" column="userId">
<generator class="native"></generator>
</id> <property name="name" column="userName"></property> <one-to-one name="address" class="com.wishwzp.model.Address" cascade="all"></one-to-one>
</class> </hibernate-mapping>
Address.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wishwzp.model"> <class name="Address" table="t_address">
<id name="id" column="addressId">
<generator class="foreign">
<param name="property">user</param>
</generator>
</id> <property name="address" column="address"></property>
<property name="zipcode" column="zipcode"></property> <one-to-one name="user" class="com.wishwzp.model.User" constrained="true"></one-to-one>
</class> </hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--数据库连接设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/wishwzp/model/User.hbm.xml"/>
<mapping resource="com/wishwzp/model/Address.hbm.xml"/> </session-factory> </hibernate-configuration>
UserTest.java
package com.wishwzp.service; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Address;
import com.wishwzp.model.Address2;
import com.wishwzp.model.User;
import com.wishwzp.model.User2;
import com.wishwzp.util.HibernateUtil; public class UserTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testSave1(){ } }
UserTest.java
package com.wishwzp.service; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Address;
import com.wishwzp.model.Address2;
import com.wishwzp.model.User;
import com.wishwzp.model.User2;
import com.wishwzp.util.HibernateUtil; public class UserTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testSave1(){
User user=new User();
user.setName("张三"); Address address=new Address();
address.setAddress("某地方");
address.setZipcode("43242");
address.setUser(user); user.setAddress(address);
session.save(user);
} }
我们发现主键一对一的。。。。
2,按照外键映射:
User2.java
package com.wishwzp.model; public class User2 { private int id;
private String name;
private Address2 address; 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 Address2 getAddress() {
return address;
}
public void setAddress(Address2 address) {
this.address = address;
} }
Address2.java
package com.wishwzp.model; public class Address2 { private int id;
private String address;
private String zipcode;
private User2 user; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public User2 getUser() {
return user;
}
public void setUser(User2 user) {
this.user = user;
} }
User2.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wishwzp.model"> <class name="User2" table="t_user2">
<id name="id" column="userId">
<generator class="native"></generator>
</id> <property name="name" column="userName"></property> <many-to-one name="address" class="com.wishwzp.model.Address2" column="addressId" cascade="all" unique="true"></many-to-one>
</class> </hibernate-mapping>
Address2.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wishwzp.model"> <class name="Address2" table="t_address2">
<id name="id" column="addressId">
<generator class="native">
</generator>
</id> <property name="address" column="address"></property>
<property name="zipcode" column="zipcode"></property> <one-to-one name="user" class="com.wishwzp.model.User2" property-ref="address"></one-to-one>
</class> </hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--数据库连接设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/wishwzp/model/User2.hbm.xml"/>
<mapping resource="com/wishwzp/model/Address2.hbm.xml"/> </session-factory> </hibernate-configuration>
UserTest.java
package com.wishwzp.service; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Address;
import com.wishwzp.model.Address2;
import com.wishwzp.model.User;
import com.wishwzp.model.User2;
import com.wishwzp.util.HibernateUtil; public class UserTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testSave2(){ } }
UserTest.java
package com.wishwzp.service; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Address;
import com.wishwzp.model.Address2;
import com.wishwzp.model.User;
import com.wishwzp.model.User2;
import com.wishwzp.util.HibernateUtil; public class UserTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testSave2(){
User2 user=new User2();
user.setName("李四"); Address2 address=new Address2();
address.setAddress("某地方2");
address.setZipcode("432422");
address.setUser(user); user.setAddress(address);
session.save(user);
} }
我们发现:t_User2表中的addressId对应着t_address2表中的主键addressId
第二节:Hibernate 多对多映射关系实现
1,多对多单向实现;
2,多对多双向实现;
1,多对多单向实现:
Course.java
package com.wishwzp.model; public class Course { 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;
} }
Student.java
package com.wishwzp.model; import java.util.HashSet;
import java.util.Set; public class Student { private int id;
private String name;
private Set<Course> courses=new HashSet<Course>(); 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<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
} }
Course.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wishwzp.model"> <class name="Course" table="t_course">
<id name="id" column="courseId">
<generator class="native"></generator>
</id> <property name="name" column="courseName"></property> </class> </hibernate-mapping>
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wishwzp.model"> <class name="Student" table="t_student">
<id name="id" column="studentId">
<generator class="native"></generator>
</id> <property name="name" column="studentName"></property> <set name="courses" table="student_course" cascade="save-update">
<key column="student_id"></key>
<many-to-many class="com.wishwzp.model.Course" column="course_id"></many-to-many>
</set>
</class> </hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--数据库连接设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/wishwzp/model/Student.hbm.xml"/>
<mapping resource="com/wishwzp/model/Course.hbm.xml"/> </session-factory> </hibernate-configuration>
StudentTest.java
package com.wishwzp.service; import java.util.Iterator;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Course;
import com.wishwzp.model.Course2;
import com.wishwzp.model.Student;
import com.wishwzp.model.Student2;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testSave1(){ } }
StudentTest.java
package com.wishwzp.service; import java.util.Iterator;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Course;
import com.wishwzp.model.Course2;
import com.wishwzp.model.Student;
import com.wishwzp.model.Student2;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testSave1(){
Course course1=new Course();
course1.setName("语文"); Course course2=new Course();
course2.setName("数学"); Student student1=new Student();
student1.setName("张三");
student1.getCourses().add(course1);
student1.getCourses().add(course2); Student student2=new Student();
student2.setName("李四");
student2.getCourses().add(course1);
student2.getCourses().add(course2); session.save(student1);
session.save(student2);
} }
我们现在查询一下数据:
StudentTest.java
package com.wishwzp.service; import java.util.Iterator;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Course;
import com.wishwzp.model.Course2;
import com.wishwzp.model.Student;
import com.wishwzp.model.Student2;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testSave1(){
Course course1=new Course();
course1.setName("语文"); Course course2=new Course();
course2.setName("数学"); Student student1=new Student();
student1.setName("张三");
student1.getCourses().add(course1);
student1.getCourses().add(course2); Student student2=new Student();
student2.setName("李四");
student2.getCourses().add(course1);
student2.getCourses().add(course2); session.save(student1);
session.save(student2);
} @Test
public void testLoad1(){
Student student=(Student)session.get(Student.class, 1);
Set<Course> courses=(Set<Course>)student.getCourses();
Iterator it=courses.iterator();
while(it.hasNext()){
Course c=(Course)it.next();
System.out.println(c.getName());
}
} }
我们这里是单向的,只能从学生这端去查找课程,无法从课程这端查找学生的,这就是单向的。
2,多对多双向实现:
Course2.java
package com.wishwzp.model; import java.util.HashSet;
import java.util.Set; public class Course2 { private int id;
private String name;
private Set<Student2> students=new HashSet<Student2>(); 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<Student2> getStudents() {
return students;
}
public void setStudents(Set<Student2> students) {
this.students = students;
} }
Student2.java
package com.wishwzp.model; import java.util.HashSet;
import java.util.Set; public class Student2 { private int id;
private String name;
private Set<Course2> courses=new HashSet<Course2>(); 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<Course2> getCourses() {
return courses;
}
public void setCourses(Set<Course2> courses) {
this.courses = courses;
} }
Course2.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wishwzp.model"> <class name="Course2" table="t_course2">
<id name="id" column="courseId">
<generator class="native"></generator>
</id> <property name="name" column="courseName"></property> <set name="students" table="student_course2" inverse="true" >
<key column="course_id"></key>
<many-to-many class="com.wishwzp.model.Student2" column="student_id"></many-to-many>
</set> </class> </hibernate-mapping>
Student2.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.wishwzp.model"> <class name="Student2" table="t_student2">
<id name="id" column="studentId">
<generator class="native"></generator>
</id> <property name="name" column="studentName"></property> <set name="courses" table="student_course2" cascade="save-update">
<key column="student_id"></key>
<many-to-many class="com.wishwzp.model.Course2" column="course_id"></many-to-many>
</set>
</class> </hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--数据库连接设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/wishwzp/model/Student2.hbm.xml"/>
<mapping resource="com/wishwzp/model/Course2.hbm.xml"/> </session-factory> </hibernate-configuration>
StudentTest.java
package com.wishwzp.service; import java.util.Iterator;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Course;
import com.wishwzp.model.Course2;
import com.wishwzp.model.Student;
import com.wishwzp.model.Student2;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testSave2(){ } }
StudentTest.java
package com.wishwzp.service; import java.util.Iterator;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Course;
import com.wishwzp.model.Course2;
import com.wishwzp.model.Student;
import com.wishwzp.model.Student2;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testSave2(){
Course2 course1=new Course2();
course1.setName("语文"); Course2 course2=new Course2();
course2.setName("数学"); Student2 student1=new Student2();
student1.setName("张三");
student1.getCourses().add(course1);
student1.getCourses().add(course2); Student2 student2=new Student2();
student2.setName("李四");
student2.getCourses().add(course1);
student2.getCourses().add(course2); session.save(student1);
session.save(student2);
} }
我们现在查询一下数据:
StudentTest.java
package com.wishwzp.service; import java.util.Iterator;
import java.util.Set; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.wishwzp.model.Course;
import com.wishwzp.model.Course2;
import com.wishwzp.model.Student;
import com.wishwzp.model.Student2;
import com.wishwzp.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
private Session session; @Before
public void setUp() throws Exception {
session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务
} @After
public void tearDown() throws Exception {
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
} @Test
public void testSave2(){
Course2 course1=new Course2();
course1.setName("语文"); Course2 course2=new Course2();
course2.setName("数学"); Student2 student1=new Student2();
student1.setName("张三");
student1.getCourses().add(course1);
student1.getCourses().add(course2); Student2 student2=new Student2();
student2.setName("李四");
student2.getCourses().add(course1);
student2.getCourses().add(course2); session.save(student1);
session.save(student2);
} @Test
public void testLoad2(){
Course2 course=(Course2)session.get(Course2.class, 1);
Set<Student2> students=(Set<Student2>)course.getStudents();
Iterator it=students.iterator();
while(it.hasNext()){
Student2 s=(Student2)it.next();
System.out.println(s.getName());
} } }
我们这里是双向的,既可以从学生这端去查找课程,又可以从课程这端查找学生的,这就是双向的。
(八)Hibernate 映射关系的更多相关文章
- Hibernate学习8—Hibernate 映射关系(多对多)
第二节:Hibernate 多对多映射关系实现 比如学生和课程是多对多的关系: 一个学生可以选多个课程: 一个课程可以被多个学生选中,所以是多对多的关系: 1,多对多单向实现: 单向关系: 这 ...
- Hibernate学习8—Hibernate 映射关系(一对一)
第一节:Hibernate 一对一映射关系实现 假设一个用户对应一个地址: 1)按照主键映射: User.java: package com.cy.model; public class User ...
- Hibernate 映射关系
映射组成关系 •建立域模型和关系数据模型有着不同的出发点: –域模型: 由程序代码组成, 通过细化持久化类的的粒度可提高代码的可重用性, 简化编程 –在没有数据冗余的情况下, 应该尽可能减少表的数目, ...
- hibernate映射关系之多对多
多对多: * 关系在第三张表中,和两张表本身没有关系 * 多对多谁维护关系:谁都能维护关系(效率是一样的),一般情况下可以通过页面 来体现 * 关系体现: 第三张表的维护:增加.删除 course类对 ...
- hibernate映射关系(多对多)
Student与Teacher关系多对多(只建了2个实体类) public class Student { private int id; private String name; private S ...
- Hibernate映射关系之_多对多
多对多关系由于效率的原因,实际中会拆成相互的一对多的关系,不再累述
- Hibernate学习2--对象的三种状态以及映射关系的简单配置
上篇hibernate的博客总体简单梳理了对象持久化的一些思想以及hibernate中对象持久化化的方法,下面说说对象持久化过程的三种状态. 一.hibernate缓存的概念 1.session与缓存 ...
- Hibernate学习之映射关系
一.Hibernate多对一关联映射:就是在“多”的一端加外键,指向“一”的一端. 比如多个学生对应一个班级,多个用户对应一个级别等等,都是多对一关系. 1.“多”端实体加入引用“一”端实体的变量及g ...
- Hibernate的七种映射关系之七种关联映射(二)
继续上篇博客 七.Hibernate双向一对多关联映射:让多的一端来维护关系. 主要是解决一对多单向关联的缺陷,而不是需求驱动的. 1.在Student.java实体类里添加Classes引用.pri ...
随机推荐
- Java管道流PipedStream
管道读取流和管道写入流可以像管道一样对接上,管道读取流就可以读取管道写入流写入的数据.需要注意的是需要加入多线程技术,因为单线程,先执行read,会发生死锁,因为read方法是阻塞式的,没有数据的re ...
- sts中从svn导入maven项目
1.创建资源库 2.导入项目作为本地项目 3.将子module从本地导入(默认情况下,只会将主pom所在的工程导入)
- (转)java读取数据库表信息,子段
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...
- Go2Shell
1.背景 windows系统可以轻而易举地拿到文件所在目录, 但是mac显得想拿文件目录有点蛋疼.而Go2Shell可以快速定位到文件所在的目录. 2.安装配置 选择默认打开的终端软件 3.使用 进入 ...
- 【转载】总结一下Android中主题(Theme)的正确玩法
http://www.cnblogs.com/zhouyou96/p/5323138.html 总结一下Android中主题(Theme)的正确玩法 在AndroidManifest.xml文件中有& ...
- sgu 101 无向图有双重边的欧拉路径
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> ...
- ThinkPHP函数详解:C方法
C方法是ThinkPHP用于设置.获取,以及保存配置参数的方法,使用频率较高.了解C方法需要首先了解下ThinkPHP的配置,因为C方法的所有操作都是围绕配置相关的.ThinkPHP的配置文件采用PH ...
- [Open Projects Series] ViewPagerTransforms
https://github.com/jfeinstein10/JazzyViewPager https://github.com/ToxicBakery/ViewPagerTransforms
- 全面解析java注解
一.注解概述 1.学习注解能够读懂别人的代码,特别是框架相关的代码 2.让自己的编程更加简洁,代码更加清晰 3.让别人高看一眼,会使用自定义注解来解决问题 ...
- SQL书写技巧
SQL书写技巧: 1.针对分区表,如果可以使用分区条件的,一定要加分区条件.分区条件的使用,可以减少不必要的数据访问,加快查询数据,如TB_CSV_ACCEPT_FLOW_OPERATOR表,以acc ...