Skip to content

Commit d527bf9

Browse files
vcharmcasterSanne
authored andcommitted
Simplified code, removed redundant method
created generic method which will save all objects such as user, skill, tool
1 parent 85b2d58 commit d527bf9

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

hibernate-orm/core/Basic/src/main/java/org/hibernate/brmeyer/demo/BasicOrmDemo.java

+8-32
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public static void main(String[] args) {
5555
Tool tool = new Tool();
5656
// Note: id generated by Hibernate
5757
tool.setName( "Hammer" );
58-
insertTool( tool );
58+
insertObject( tool );
5959
List<Tool> tools = new ArrayList<Tool>();
6060
tools.add( tool );
6161

6262
Skill skill = new Skill();
6363
// Note: id generated by Hibernate
6464
skill.setName( "Hammering Things" );
65-
insertSkill( skill );
65+
insertObject( skill );
6666
List<Skill> skills = new ArrayList<Skill>();
6767
skills.add( skill );
6868

@@ -74,7 +74,7 @@ public static void main(String[] args) {
7474
user.setTools( tools );
7575
user.setSkills( skills );
7676

77-
insertUser( user );
77+
insertObject( user );
7878

7979
user = getUser( user.getId() );
8080
System.out.println( user.toString() );
@@ -85,43 +85,19 @@ public static void main(String[] args) {
8585
}
8686

8787
/**
88-
* Insert user.
88+
* Insert Object.
8989
*
90-
* @param user the user
90+
* @param object the object
9191
* @throws Exception the exception
9292
*/
93-
private static void insertUser(User user) throws Exception {
93+
private static void insertObject(Object object) throws Exception {
9494
Session session = openSession();
9595
session.getTransaction().begin();
96-
session.persist( user ); // cascades the tool & skill relationships
96+
/* persist object such as tool, user, skill and their relationship */
97+
session.persist( object );
9798
session.getTransaction().commit();
9899
}
99100

100-
/**
101-
* Insert tool.
102-
*
103-
* @param tool the tool
104-
* @throws SQLException the SQL exception
105-
*/
106-
private static void insertTool(Tool tool) throws SQLException {
107-
Session session = openSession();
108-
session.getTransaction().begin();
109-
session.persist( tool );
110-
session.getTransaction().commit();
111-
}
112-
113-
/**
114-
* Insert skill.
115-
*
116-
* @param skill the skill
117-
* @throws SQLException the SQL exception
118-
*/
119-
private static void insertSkill(Skill skill) throws SQLException {
120-
Session session = openSession();
121-
session.getTransaction().begin();
122-
session.persist( skill );
123-
session.getTransaction().commit();
124-
}
125101

126102
/**
127103
* Gets the user.

0 commit comments

Comments
 (0)