From 942b186e60ac6a7b4cef12b1aad1b8a882ace36c Mon Sep 17 00:00:00 2001 From: egmp777 Date: Sat, 10 May 2014 15:13:30 -0500 Subject: [PATCH] Hibernate/JPA Sorting Repo Compiled in Eclipse --- spring-hibernate4/.project | 6 +++ .../org.hibernate.eclipse.console.prefs | 3 ++ .../baeldung/persistence/model/Bar.hbm.xml | 21 ++++++++ .../org/baeldung/persistence/model/Bar.java | 8 +-- .../baeldung/persistence/model/Foo.hbm.xml | 21 ++++++++ .../org/baeldung/persistence/model/Foo.java | 45 ++++++++++------ .../src/test/java/hibernate.cfg.xml | 37 +++++++++++++ .../service/FooSortingServiceTest.java | 48 ++++++----------- spring-jpa/.classpath | 6 +-- spring-jpa/.project | 6 +++ .../org.hibernate.eclipse.console.prefs | 3 ++ .../org/baeldung/persistence/model/Bar.java | 52 ++++++++++--------- .../org/baeldung/persistence/model/Foo.java | 34 ++++++++---- .../src/test/java/META-INF/persistence.xml | 16 ++++++ .../service/FooServiceSortingTests.java | 13 +++-- 15 files changed, 225 insertions(+), 94 deletions(-) create mode 100644 spring-hibernate4/.settings/org.hibernate.eclipse.console.prefs create mode 100644 spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.hbm.xml create mode 100644 spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml create mode 100644 spring-hibernate4/src/test/java/hibernate.cfg.xml create mode 100644 spring-jpa/.settings/org.hibernate.eclipse.console.prefs create mode 100644 spring-jpa/src/test/java/META-INF/persistence.xml diff --git a/spring-hibernate4/.project b/spring-hibernate4/.project index b687191646..10df76aa23 100644 --- a/spring-hibernate4/.project +++ b/spring-hibernate4/.project @@ -30,6 +30,11 @@ + + org.hibernate.eclipse.console.hibernateBuilder + + + org.springframework.ide.eclipse.core.springnature @@ -39,5 +44,6 @@ org.eclipse.m2e.core.maven2Nature org.eclipse.wst.common.project.facet.core.nature org.eclipse.wst.jsdt.core.jsNature + org.hibernate.eclipse.console.hibernateNature diff --git a/spring-hibernate4/.settings/org.hibernate.eclipse.console.prefs b/spring-hibernate4/.settings/org.hibernate.eclipse.console.prefs new file mode 100644 index 0000000000..7505c63770 --- /dev/null +++ b/spring-hibernate4/.settings/org.hibernate.eclipse.console.prefs @@ -0,0 +1,3 @@ +default.configuration= +eclipse.preferences.version=1 +hibernate3.enabled=true diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.hbm.xml b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.hbm.xml new file mode 100644 index 0000000000..bbfbb73329 --- /dev/null +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.hbm.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java index 0489dbc21a..cba3173f2f 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Bar.java @@ -36,8 +36,8 @@ public class Bar implements Serializable { public Set getFooSet() { return fooSet; } - - public void setFooList(Set fooSet) { + + public void setFooSet(final Set fooSet) { this.fooSet = fooSet; } @@ -45,7 +45,7 @@ public class Bar implements Serializable { return this.id; } - public void setId(int id) { + public void setId(final int id) { this.id = id; } @@ -53,7 +53,7 @@ public class Bar implements Serializable { return this.name; } - public void setName(String name) { + public void setName(final String name) { this.name = name; } // diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml new file mode 100644 index 0000000000..09922c8783 --- /dev/null +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.hbm.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java index 8e1dee33e8..29e9b332ec 100644 --- a/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-hibernate4/src/main/java/org/baeldung/persistence/model/Foo.java @@ -2,46 +2,61 @@ package org.baeldung.persistence.model; import java.io.Serializable; -import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; @Entity public class Foo implements Serializable { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - - @Column(nullable = false) - private String name; + private static final long serialVersionUID = 1L; public Foo() { super(); + + } + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + private String name; + @ManyToOne(targetEntity = Bar.class) + @JoinColumn(name = "BAR_ID") + @Fetch(FetchMode.JOIN) + private Bar bar = new Bar(); + + public Bar getBar() { + return bar; } - public Foo(final String name) { - super(); - - this.name = name; + public void setBar(final Bar bar) { + this.bar = bar; } - // API + public int getBar_Id() { + return bar_Id; + } + + public void setBar_Id(final int bar_Id) { + this.bar_Id = bar_Id; + } + + private int bar_Id; public long getId() { return id; } - public void setId(final long id) { this.id = id; } - public String getName() { return name; } - public void setName(final String name) { this.name = name; } diff --git a/spring-hibernate4/src/test/java/hibernate.cfg.xml b/spring-hibernate4/src/test/java/hibernate.cfg.xml new file mode 100644 index 0000000000..915295111e --- /dev/null +++ b/spring-hibernate4/src/test/java/hibernate.cfg.xml @@ -0,0 +1,37 @@ + + + + + + + + +com.mysql.jdbc.Driver +jdbc:mysql://localhost:3306/HIBERTEST2_TEST +root + + + +1 + + +org.hibernate.dialect.MySQLDialect + + +thread + + +org.hibernate.cache.internal.NoCacheProvider + + +true + + + + + + + + diff --git a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java index 9d6f3c355a..b808a5e36b 100644 --- a/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java +++ b/spring-hibernate4/src/test/java/org/baeldung/persistence/service/FooSortingServiceTest.java @@ -1,26 +1,13 @@ package org.baeldung.persistence.service; -import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.junit.Assert.assertNull; -import org.baeldung.persistence.model.Foo; -import org.baeldung.persistence.service.IFooService; -import org.baeldung.spring.PersistenceConfig; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.dao.InvalidDataAccessApiUsageException; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; -import static org.junit.Assert.*; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.After; import java.util.List; import java.util.Set; + +import org.baeldung.persistence.model.Bar; +import org.baeldung.persistence.model.Foo; +import org.baeldung.spring.PersistenceConfig; import org.hibernate.Criteria; import org.hibernate.NullPrecedence; import org.hibernate.Query; @@ -29,8 +16,14 @@ import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Order; -import com.cc.example.hibernate.Foo; -import com.cc.example.hibernate.Bar; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) @@ -81,7 +74,6 @@ public class FooSortingServiceTest { System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); } sess.getTransaction().commit(); - } @Test @@ -96,7 +88,6 @@ public class FooSortingServiceTest { } sess.getTransaction().commit(); - } @Test @@ -108,10 +99,9 @@ public class FooSortingServiceTest { for (final Foo foo : fooList) { System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() - ); + ); } sess.getTransaction().commit(); - } @Test @@ -123,10 +113,9 @@ public class FooSortingServiceTest { for (final Foo foo : fooList) { System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId() - ); + ); } sess.getTransaction().commit(); - } @Test @@ -139,7 +128,6 @@ public class FooSortingServiceTest { System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); } sess.getTransaction().commit(); - } @Test @@ -152,7 +140,6 @@ public class FooSortingServiceTest { System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); } sess.getTransaction().commit(); - } @Test @@ -166,7 +153,6 @@ public class FooSortingServiceTest { System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); } sess.getTransaction().commit(); - } @Test @@ -178,10 +164,8 @@ public class FooSortingServiceTest { assertNull(fooList.get(fooList.toArray().length - 1).getName()); for (final Foo foo : fooList) { System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); - } sess.getTransaction().commit(); - } @Test @@ -206,7 +190,7 @@ public class FooSortingServiceTest { final Query query = sess.createQuery(hql); final List barList = query.list(); for (final Bar bar : barList) { - final Set fooSet = bar.getFooList(); + final Set fooSet = bar.getFooSet(); System.out.println("Bar Id:" + bar.getId()); for (final Foo foo : fooSet) { System.out.println("FooName:" + foo.getName()); diff --git a/spring-jpa/.classpath b/spring-jpa/.classpath index ca257cf1f9..d1d54e092a 100644 --- a/spring-jpa/.classpath +++ b/spring-jpa/.classpath @@ -22,11 +22,7 @@ - - - - - + diff --git a/spring-jpa/.project b/spring-jpa/.project index 235ae29ecf..5bb2baa2b9 100644 --- a/spring-jpa/.project +++ b/spring-jpa/.project @@ -30,6 +30,11 @@ + + org.hibernate.eclipse.console.hibernateBuilder + + + org.springframework.ide.eclipse.core.springnature @@ -39,5 +44,6 @@ org.eclipse.m2e.core.maven2Nature org.eclipse.wst.common.project.facet.core.nature org.eclipse.wst.jsdt.core.jsNature + org.hibernate.eclipse.console.hibernateNature diff --git a/spring-jpa/.settings/org.hibernate.eclipse.console.prefs b/spring-jpa/.settings/org.hibernate.eclipse.console.prefs new file mode 100644 index 0000000000..7505c63770 --- /dev/null +++ b/spring-jpa/.settings/org.hibernate.eclipse.console.prefs @@ -0,0 +1,3 @@ +default.configuration= +eclipse.preferences.version=1 +hibernate3.enabled=true diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java index c2da09957f..03162b8447 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/Bar.java @@ -1,65 +1,69 @@ package org.baeldung.persistence.model; import java.io.Serializable; -import javax.persistence.Column; +import java.util.List; + +import javax.persistence.CascadeType; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.CascadeType; +import javax.persistence.OneToMany; +import javax.persistence.OrderBy; @Entity public class Bar implements Serializable { - + private static final long serialVersionUID = 1L; - + @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; - + @OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL) @OrderBy("name ASC") List fooList; - + private String name; - + public Bar(){ super(); } - + public Bar(final String name){ super(); this.name = name; } //API - + public List getFooList() { return fooList; } - - public void setFooList(List fooList) { + + public void setFooList(final List fooList) { this.fooList = fooList; } - + public int getId() { - return this.id; + return id; } - - public void setId(int id) { + + public void setId(final int id) { this.id = id; } - + public String getName() { - return this.name; + return name; } - - public void setName(String name) { + + public void setName(final String name) { this.name = name; } - + // - + @Override public int hashCode() { final int prime = 31; @@ -67,7 +71,7 @@ public class Bar implements Serializable { result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } - + @Override public boolean equals(final Object obj) { if (this == obj) @@ -84,12 +88,12 @@ public class Bar implements Serializable { return false; return true; } - + @Override public String toString() { final StringBuilder builder = new StringBuilder(); builder.append("Bar [name=").append(name).append("]"); return builder.toString(); } - + } diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java index aed28cc07f..585cefb159 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java +++ b/spring-jpa/src/main/java/org/baeldung/persistence/model/Foo.java @@ -1,21 +1,20 @@ package org.baeldung.persistence.model; import java.io.Serializable; + import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; @Entity public class Foo implements Serializable { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private long id; - - @Column(nullable = false) - private String name; + private static final long serialVersionUID = 1L; public Foo() { super(); @@ -27,13 +26,30 @@ public class Foo implements Serializable { this.name = name; } - // API + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "ID") + private long id; + @Column(name = "NAME") + private String name; + + @ManyToOne(targetEntity = Bar.class, fetch = FetchType.EAGER) + @JoinColumn(name = "BAR_ID") + private Bar bar; + + public Bar getBar() { + return bar; + } + + public void setBar(final Bar bar) { + this.bar = bar; + } public long getId() { return id; } - public void setId(final long id) { + public void setId(final int id) { this.id = id; } @@ -45,8 +61,6 @@ public class Foo implements Serializable { this.name = name; } - // - @Override public int hashCode() { final int prime = 31; diff --git a/spring-jpa/src/test/java/META-INF/persistence.xml b/spring-jpa/src/test/java/META-INF/persistence.xml new file mode 100644 index 0000000000..e528491795 --- /dev/null +++ b/spring-jpa/src/test/java/META-INF/persistence.xml @@ -0,0 +1,16 @@ + + + + org.baeldung.persistence.model.Foo + org.baeldung.persistence.model.Bar + + + + + + + + + + + diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java index e1ecbd2015..1a15faf2c1 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/service/FooServiceSortingTests.java @@ -1,7 +1,9 @@ package org.baeldung.persistence.service; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNull; + import java.util.List; + import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; @@ -11,11 +13,13 @@ import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; -import com.google.common.collect.Lists; + +import org.baeldung.persistence.model.Bar; +import org.baeldung.persistence.model.Foo; import org.junit.BeforeClass; import org.junit.Test; -import com.cc.jpa.example.Foo; -import com.cc.jpa.example.Bar; + +import com.google.common.collect.Lists; public class FooServiceSortingTests { private static EntityManager entityManager; @@ -162,4 +166,5 @@ public class FooServiceSortingTests { } } + }