Skip to content

Commit c5905f0

Browse files
committed
hibernate oneToMany, ManyToOne, ManyToMany
1 parent 72adf63 commit c5905f0

18 files changed

+560
-0
lines changed

Hibernate/entityManyToMany/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/test/java=UTF-8
4+
encoding/<project>=UTF-8
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.6
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
12+
org.eclipse.jdt.core.compiler.source=1.6
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
default.configuration=
2+
eclipse.preferences.version=1
3+
hibernate3.enabled=true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
org.jboss.ide.eclipse.as.core.singledeployable.deployableList=

Hibernate/entityManyToMany/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.imooc</groupId>
6+
<artifactId>entity</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>entity</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.hibernate</groupId>
20+
<artifactId>hibernate-core</artifactId>
21+
<version>4.1.0.Final</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
<version>4.10</version>
27+
<scope>test</scope>
28+
</dependency>
29+
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
30+
<dependency>
31+
<groupId>mysql</groupId>
32+
<artifactId>mysql-connector-java</artifactId>
33+
<version>5.1.40</version>
34+
</dependency>
35+
</dependencies>
36+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3+
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4+
<!-- Generated 2017-2-7 21:07:07 by Hibernate Tools 3.4.0.CR1 -->
5+
<hibernate-mapping>
6+
<class name="com.imooc.entity.Employee" table="employee">
7+
<id name="empid" type="int">
8+
<column name="empid" />
9+
<generator class="assigned" />
10+
</id>
11+
<property name="empname" type="java.lang.String">
12+
<column name="empname" length="20" not-null="true" />
13+
</property>
14+
<set name="projects" table="proemp">
15+
<key>
16+
<column name="rempid" />
17+
</key>
18+
<many-to-many class="com.imooc.entity.Project" column="rproid" />
19+
</set>
20+
</class>
21+
</hibernate-mapping>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.imooc.entity;
2+
3+
import java.io.Serializable;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class Employee implements Serializable {
8+
private int empid;
9+
private String empname;
10+
11+
// 添加一个项目的集合
12+
private Set<Project> projects = new HashSet<Project>();
13+
14+
public Employee() {
15+
// TODO Auto-generated constructor stub
16+
}
17+
18+
public Employee(int empid, String empname) {
19+
this.empid = empid;
20+
this.empname = empname;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "Employee [empid=" + empid + ", empname=" + empname + "]";
26+
}
27+
28+
public int getEmpid() {
29+
return empid;
30+
}
31+
32+
public void setEmpid(int empid) {
33+
this.empid = empid;
34+
}
35+
36+
public String getEmpname() {
37+
return empname;
38+
}
39+
40+
public void setEmpname(String empname) {
41+
this.empname = empname;
42+
}
43+
44+
public Set<Project> getProjects() {
45+
return projects;
46+
}
47+
48+
public void setProjects(Set<Project> projects) {
49+
this.projects = projects;
50+
}
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3+
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4+
<!-- Generated 2017-2-7 10:25:55 by Hibernate Tools 3.4.0.CR1 -->
5+
<hibernate-mapping>
6+
<class name="com.imooc.entity.Grade" table="grade">
7+
<id name="gid" type="int">
8+
<column name="gid" />
9+
<generator class="increment" />
10+
</id>
11+
<property name="gname" type="java.lang.String">
12+
<column name="gname" length="20" not-null="true" />
13+
</property>
14+
<property name="gdesc" type="java.lang.String">
15+
<column name="gdesc" />
16+
</property>
17+
<set name="students" table="student" inverse="false" lazy="true" cascade="save-update">
18+
<key>
19+
<column name="gid" />
20+
</key>
21+
<one-to-many class="com.imooc.entity.Student" />
22+
</set>
23+
</class>
24+
</hibernate-mapping>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.imooc.entity;
2+
3+
import java.io.Serializable;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class Grade implements Serializable {
8+
private int gid;
9+
private String gname;
10+
private String gdesc;
11+
12+
//
13+
private Set<Student> students = new HashSet<Student>();
14+
15+
public int getGid() {
16+
return gid;
17+
}
18+
19+
public void setGid(int gid) {
20+
this.gid = gid;
21+
}
22+
23+
public String getGname() {
24+
return gname;
25+
}
26+
27+
public void setGname(String gname) {
28+
this.gname = gname;
29+
}
30+
31+
public String getGdesc() {
32+
return gdesc;
33+
}
34+
35+
public void setGdesc(String gdesc) {
36+
this.gdesc = gdesc;
37+
}
38+
39+
public Set<Student> getStudents() {
40+
return students;
41+
}
42+
43+
public void setStudents(Set<Student> students) {
44+
this.students = students;
45+
}
46+
47+
public Grade(String gname, String gdesc) {
48+
this.gname = gname;
49+
this.gdesc = gdesc;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "Grade [gid=" + gid + ", gname=" + gname + ", gdesc=" + gdesc
55+
+ ", students=" + students + "]";
56+
}
57+
58+
public Grade() {
59+
// TODO Auto-generated constructor stub
60+
}
61+
62+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3+
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4+
<!-- Generated 2017-2-7 21:07:07 by Hibernate Tools 3.4.0.CR1 -->
5+
<hibernate-mapping>
6+
<class name="com.imooc.entity.Project" table="project">
7+
<id name="proid" type="int">
8+
<column name="proid" />
9+
<generator class="assigned" />
10+
</id>
11+
<property name="proname" type="java.lang.String">
12+
<column name="proname" />
13+
</property>
14+
<!-- 配置多对多的关系 -->
15+
<set name="employees" table="proemp" cascade="all">
16+
<key>
17+
<column name="rproid" />
18+
</key>
19+
<many-to-many class="com.imooc.entity.Employee" column="rempid" />
20+
</set>
21+
</class>
22+
</hibernate-mapping>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.imooc.entity;
2+
3+
import java.io.Serializable;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class Project implements Serializable {
8+
private int proid;
9+
private String proname;
10+
11+
// 添加一个员工集合
12+
private Set<Employee> employees = new HashSet<Employee>();
13+
14+
public Project() {
15+
// TODO Auto-generated constructor stub
16+
}
17+
18+
public Project(int proid, String proname) {
19+
this.proid = proid;
20+
this.proname = proname;
21+
}
22+
23+
public int getProid() {
24+
return proid;
25+
}
26+
27+
public void setProid(int proid) {
28+
this.proid = proid;
29+
}
30+
31+
public String getProname() {
32+
return proname;
33+
}
34+
35+
public void setProname(String proname) {
36+
this.proname = proname;
37+
}
38+
39+
40+
public Set<Employee> getEmployees() {
41+
return employees;
42+
}
43+
44+
public void setEmployees(Set<Employee> employees) {
45+
this.employees = employees;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "Project [proid=" + proid + ", proname=" + proname + "]";
51+
}
52+
53+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3+
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4+
<!-- Generated 2017-2-7 12:06:18 by Hibernate Tools 3.4.0.CR1 -->
5+
<hibernate-mapping>
6+
<class name="com.imooc.entity.Student" table="student">
7+
<id name="sid" type="int">
8+
<column name="sid" />
9+
<generator class="increment" />
10+
</id>
11+
<property name="sname" type="java.lang.String">
12+
<column name="sname" />
13+
</property>
14+
<property name="sex" type="java.lang.String">
15+
<column name="sex" />
16+
</property>
17+
<many-to-one name="grade" class="com.imooc.entity.Grade">
18+
<column name="gid" />
19+
</many-to-one>
20+
</class>
21+
</hibernate-mapping>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.imooc.entity;
2+
3+
import java.io.Serializable;
4+
5+
public class Student implements Serializable {
6+
private int sid;
7+
private String sname;
8+
private String sex;
9+
private Grade grade;
10+
11+
public Grade getGrade() {
12+
return grade;
13+
}
14+
15+
public void setGrade(Grade grade) {
16+
this.grade = grade;
17+
}
18+
19+
public Student() {
20+
// TODO Auto-generated constructor stub
21+
}
22+
23+
public Student(String sname, String sex) {
24+
this.sname = sname;
25+
this.sex = sex;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "Student [sid=" + sid + ", sname=" + sname + ", sex=" + sex
31+
+ "]";
32+
}
33+
34+
public int getSid() {
35+
return sid;
36+
}
37+
38+
public void setSid(int sid) {
39+
this.sid = sid;
40+
}
41+
42+
public String getSname() {
43+
return sname;
44+
}
45+
46+
public void setSname(String sname) {
47+
this.sname = sname;
48+
}
49+
50+
public String getSex() {
51+
return sex;
52+
}
53+
54+
public void setSex(String sex) {
55+
this.sex = sex;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)