Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spring-hibernate4/.project
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
Expand All @@ -39,5 +44,6 @@
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
default.configuration=
eclipse.preferences.version=1
hibernate3.enabled=true
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,24 @@ public Bar(final String name) {
public Set<Foo> getFooSet() {
return fooSet;
}

public void setFooList(Set<Foo> fooSet) {
public void setFooSet(final Set<Foo> fooSet) {
this.fooSet = fooSet;
}

public int getId() {
return this.id;
}

public void setId(int id) {
public void setId(final int id) {
this.id = id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
public void setName(final String name) {
this.name = name;
}
//
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,61 @@

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 {

private static final long serialVersionUID = 1L;

public Foo() {
super();

}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(nullable = false)
private String name;
@ManyToOne(targetEntity = Bar.class)
@JoinColumn(name = "BAR_ID")
@Fetch(FetchMode.JOIN)
private Bar bar = new Bar();

public Foo() {
super();
public Bar getBar() {
return bar;
}

public Foo(final String name) {
super();
public void setBar(final Bar bar) {
this.bar = bar;
}

this.name = name;
public int getBar_Id() {
return bar_Id;
}

public void setBar_Id(final int bar_Id) {
this.bar_Id = bar_Id;
}

// API
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;
}
Expand Down
37 changes: 37 additions & 0 deletions spring-hibernate4/src/test/java/hibernate.cfg.xml
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public final void whenHQlSortingByStringNullLast_thenLastNull() {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
}
sess.getTransaction().commit();

}

@Test
Expand All @@ -88,7 +87,6 @@ public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() {

}
sess.getTransaction().commit();

}

@Test
Expand All @@ -100,10 +98,9 @@ public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSorted
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()

);
);
}
sess.getTransaction().commit();

}

@Test
Expand All @@ -115,10 +112,9 @@ public final void whenHQlSortingByMultipleAttributes_thenSortedResults() {
for (final Foo foo : fooList) {
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()

);
);
}
sess.getTransaction().commit();

}

@Test
Expand All @@ -131,7 +127,6 @@ public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrint
System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId());
}
sess.getTransaction().commit();

}

@Test
Expand All @@ -144,7 +139,6 @@ public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
}
sess.getTransaction().commit();

}

@Test
Expand All @@ -158,7 +152,6 @@ public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() {
System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName());
}
sess.getTransaction().commit();

}

@Test
Expand All @@ -170,10 +163,8 @@ public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() {
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
Expand All @@ -198,7 +189,7 @@ public final void whenSortingBars_thenBarsWithSortedFoos() {
final Query query = sess.createQuery(hql);
final List<Bar> barList = query.list();
for (final Bar bar : barList) {
final Set<Foo> fooSet = bar.getFooList();
final Set<Foo> fooSet = bar.getFooSet();
System.out.println("Bar Id:" + bar.getId());
for (final Foo foo : fooSet) {
System.out.println("FooName:" + foo.getName());
Expand Down
6 changes: 1 addition & 5 deletions spring-jpa/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
Expand Down
6 changes: 6 additions & 0 deletions spring-jpa/.project
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.hibernate.eclipse.console.hibernateBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.springframework.ide.eclipse.core.springnature</nature>
Expand All @@ -39,5 +44,6 @@
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
<nature>org.hibernate.eclipse.console.hibernateNature</nature>
</natures>
</projectDescription>
3 changes: 3 additions & 0 deletions spring-jpa/.settings/org.hibernate.eclipse.console.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
default.configuration=
eclipse.preferences.version=1
hibernate3.enabled=true
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

@Entity
public class Bar implements Serializable {
private static final long serialVersionUID = 1L;


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand All @@ -23,10 +25,12 @@ public class Bar implements Serializable {
@Column(nullable = false)
private String name;


@OneToMany(mappedBy = "bar", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@OrderBy("name ASC")
List<Foo> fooList;


public Bar() {
super();
}
Expand All @@ -44,6 +48,7 @@ public long getId() {
}

public void setId(final long id) {

this.id = id;
}

Expand Down
Loading