Hibernate/JPA Sorting Repo Compiled in Eclipse

This commit is contained in:
egmp777 2014-05-10 15:13:30 -05:00
parent 08aa91739e
commit 942b186e60
15 changed files with 225 additions and 94 deletions

View File

@ -30,6 +30,11 @@
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand>
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature> <nature>org.springframework.ide.eclipse.core.springnature</nature>
@ -39,5 +44,6 @@
<nature>org.eclipse.m2e.core.maven2Nature</nature> <nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature> <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature> <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

View File

@ -0,0 +1,3 @@
default.configuration=
eclipse.preferences.version=1
hibernate3.enabled=true

View File

@ -0,0 +1,21 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 29-abr-2014 15:39:59 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="org.baeldung.persistence.model.Bar" table="BAR">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<set name="fooSet" table="FOO" inverse="false" lazy="true" order-by="NAME DESC">
<key>
<column name="BAR_ID" />
</key>
<one-to-many class="org.baeldung.persistence.model.Foo" />
</set>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
</class>
</hibernate-mapping>

View File

@ -37,7 +37,7 @@ public class Bar implements Serializable {
return fooSet; return fooSet;
} }
public void setFooList(Set<Foo> fooSet) { public void setFooSet(final Set<Foo> fooSet) {
this.fooSet = fooSet; this.fooSet = fooSet;
} }
@ -45,7 +45,7 @@ public class Bar implements Serializable {
return this.id; return this.id;
} }
public void setId(int id) { public void setId(final int id) {
this.id = id; this.id = id;
} }
@ -53,7 +53,7 @@ public class Bar implements Serializable {
return this.name; return this.name;
} }
public void setName(String name) { public void setName(final String name) {
this.name = name; this.name = name;
} }
// //

View File

@ -0,0 +1,21 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 23-abr-2014 18:00:30 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="org.baeldung.persistence.model.Foo" table="FOO">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<many-to-one name="bar" class="org.baeldung.persistence.model.Bar" fetch="join" >
<column name="BAR_ID" />
</many-to-one>
</class>
</hibernate-mapping>

View File

@ -2,46 +2,61 @@ package org.baeldung.persistence.model;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
@Entity @Entity
public class Foo implements Serializable { public class Foo implements Serializable {
@Id private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String name;
public Foo() { public Foo() {
super(); 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) { public void setBar(final Bar bar) {
super(); this.bar = bar;
this.name = name;
} }
// 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() { public long getId() {
return id; return id;
} }
public void setId(final long id) { public void setId(final long id) {
this.id = id; this.id = id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(final String name) { public void setName(final String name) {
this.name = name; this.name = name;
} }

View File

@ -0,0 +1,37 @@
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/HIBERTEST2_TEST</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<mapping resource="org//baeldung//persistence//model//Foo.hbm.xml"/>
<mapping resource="org//baeldung//persistence//model//Bar.hbm.xml"/>
</session-factory>
</hibernate-configuration>

View File

@ -1,26 +1,13 @@
package org.baeldung.persistence.service; 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.List;
import java.util.Set; 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.Criteria;
import org.hibernate.NullPrecedence; import org.hibernate.NullPrecedence;
import org.hibernate.Query; import org.hibernate.Query;
@ -29,8 +16,14 @@ import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order; import org.hibernate.criterion.Order;
import com.cc.example.hibernate.Foo; import org.junit.After;
import com.cc.example.hibernate.Bar; 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) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ -81,7 +74,6 @@ public class FooSortingServiceTest {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
} }
sess.getTransaction().commit(); sess.getTransaction().commit();
} }
@Test @Test
@ -96,7 +88,6 @@ public class FooSortingServiceTest {
} }
sess.getTransaction().commit(); sess.getTransaction().commit();
} }
@Test @Test
@ -111,7 +102,6 @@ public class FooSortingServiceTest {
); );
} }
sess.getTransaction().commit(); sess.getTransaction().commit();
} }
@Test @Test
@ -126,7 +116,6 @@ public class FooSortingServiceTest {
); );
} }
sess.getTransaction().commit(); sess.getTransaction().commit();
} }
@Test @Test
@ -139,7 +128,6 @@ public class FooSortingServiceTest {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
} }
sess.getTransaction().commit(); sess.getTransaction().commit();
} }
@Test @Test
@ -152,7 +140,6 @@ public class FooSortingServiceTest {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
} }
sess.getTransaction().commit(); sess.getTransaction().commit();
} }
@Test @Test
@ -166,7 +153,6 @@ public class FooSortingServiceTest {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
} }
sess.getTransaction().commit(); sess.getTransaction().commit();
} }
@Test @Test
@ -178,10 +164,8 @@ public class FooSortingServiceTest {
assertNull(fooList.get(fooList.toArray().length - 1).getName()); assertNull(fooList.get(fooList.toArray().length - 1).getName());
for (final Foo foo : fooList) { for (final Foo foo : fooList) {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
} }
sess.getTransaction().commit(); sess.getTransaction().commit();
} }
@Test @Test
@ -206,7 +190,7 @@ public class FooSortingServiceTest {
final Query query = sess.createQuery(hql); final Query query = sess.createQuery(hql);
final List<Bar> barList = query.list(); final List<Bar> barList = query.list();
for (final Bar bar : barList) { for (final Bar bar : barList) {
final Set<Foo> fooSet = bar.getFooList(); final Set<Foo> fooSet = bar.getFooSet();
System.out.println("Bar Id:" + bar.getId()); System.out.println("Bar Id:" + bar.getId());
for (final Foo foo : fooSet) { for (final Foo foo : fooSet) {
System.out.println("FooName:" + foo.getName()); System.out.println("FooName:" + foo.getName());

View File

@ -22,11 +22,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>

View File

@ -30,6 +30,11 @@
<arguments> <arguments>
</arguments> </arguments>
</buildCommand> </buildCommand>
<buildCommand>
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec> </buildSpec>
<natures> <natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature> <nature>org.springframework.ide.eclipse.core.springnature</nature>
@ -39,5 +44,6 @@
<nature>org.eclipse.m2e.core.maven2Nature</nature> <nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature> <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature> <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

View File

@ -0,0 +1,3 @@
default.configuration=
eclipse.preferences.version=1
hibernate3.enabled=true

View File

@ -1,12 +1,16 @@
package org.baeldung.persistence.model; package org.baeldung.persistence.model;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.Column; import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.CascadeType; import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
@Entity @Entity
public class Bar implements Serializable { public class Bar implements Serializable {
@ -38,23 +42,23 @@ public class Bar implements Serializable {
return fooList; return fooList;
} }
public void setFooList(List<Foo> fooList) { public void setFooList(final List<Foo> fooList) {
this.fooList = fooList; this.fooList = fooList;
} }
public int getId() { public int getId() {
return this.id; return id;
} }
public void setId(int id) { public void setId(final int id) {
this.id = id; this.id = id;
} }
public String getName() { public String getName() {
return this.name; return name;
} }
public void setName(String name) { public void setName(final String name) {
this.name = name; this.name = name;
} }

View File

@ -1,21 +1,20 @@
package org.baeldung.persistence.model; package org.baeldung.persistence.model;
import java.io.Serializable; import java.io.Serializable;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity @Entity
public class Foo implements Serializable { public class Foo implements Serializable {
@Id private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String name;
public Foo() { public Foo() {
super(); super();
@ -27,13 +26,30 @@ public class Foo implements Serializable {
this.name = name; 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() { public long getId() {
return id; return id;
} }
public void setId(final long id) { public void setId(final int id) {
this.id = id; this.id = id;
} }
@ -45,8 +61,6 @@ public class Foo implements Serializable {
this.name = name; this.name = name;
} }
//
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="punit" >
<class>org.baeldung.persistence.model.Foo</class>
<class>org.baeldung.persistence.model.Bar</class>
<properties>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/HIBERTEST"/>
<property name="javax.persistence.ddl-generation" value="drop-and-create-tables"/>
<property name="javax.persistence.logging.level" value="INFO"/>
<property name = "hibernate.show_sql" value = "true" />
</properties>
</persistence-unit>
</persistence>

View File

@ -1,7 +1,9 @@
package org.baeldung.persistence.service; package org.baeldung.persistence.service;
import static org.junit.Assert.*; import static org.junit.Assert.assertNull;
import java.util.List; import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction; import javax.persistence.EntityTransaction;
@ -11,11 +13,13 @@ import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; 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.BeforeClass;
import org.junit.Test; 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 { public class FooServiceSortingTests {
private static EntityManager entityManager; private static EntityManager entityManager;
@ -162,4 +166,5 @@ public class FooServiceSortingTests {
} }
} }
} }