0

I am porting a java fat client from JDBC SQLite to Hibernate H2.

Until now I tried to separate all my database code in separate classes, like TableTeam or TableMember, which have methods like .getAllTeams() or .updateTeam(Team t). These methods acted as a wrapper around their sql queries they executed.

Now with hibernate I tried to leave the interface as good as possible and just change the SQL queries to hibernate functions, which mostly works. With one exception: updating elements.

team = new Team(-1, "Team Name", Collections.<Member>emptySet());

@Test
public void testUpdateTeam() throws Exception {
    table.addTeam(team);
    team.setName("New Name");
    table.updateTeam(team);
    Team f = table.getAllTeams().get(0);
    Assert.assertEquals(team, f);
}

results in the following exception:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ch.tiim.sco.database.model.Team.members, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:576)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:215)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:156)
at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:160)
at java.util.AbstractSet.equals(AbstractSet.java:92)
at org.hibernate.collection.internal.PersistentSet.equals(PersistentSet.java:441)
at ch.tiim.sco.database.model.Team.equals(Team.java:81)
at org.junit.Assert.isEquals(Assert.java:131)
at org.junit.Assert.equalsRegardingNull(Assert.java:127)
at org.junit.Assert.assertEquals(Assert.java:111)
at org.junit.Assert.assertEquals(Assert.java:144)
at ch.tiim.sco.database.TableMemberTest.testEditTeam(TableMemberTest.java:45)

The code for .updateTeam(Team t) is the following:

public void updateTeam(Team t) {
    Session s = sessionFactory.getCurrentSession();
    s.beginTransaction();
    s.update(t);
    s.getTransaction().commit();
}

And the same for .addTeam(Team t) but s.save(t) instead of s.update(t)

And Team looks like this:

@Entity
@Table(name = "team")
public class Team implements Model {
    @Id
    @GeneratedValue
    @Column(name = "team_id")
    private int id;
    @Column(name = "name")
    private String name;
    @ManyToOne
    @JoinColumn(name = "club_id")
    private Club club;
    @ManyToMany
    @JoinTable(name = "team_members",
            joinColumns = {@JoinColumn(name = "team_id")},
            inverseJoinColumns = {@JoinColumn(name = "member_id")}
    )
    private java.util.Set<Member> members;

    //I stripped the constructor and the setters/getters
}

Now how do I get rid of this error, without guaranteeing that every call to hibernate is from the same thread?

Edit Here is the hibernate configuration

<?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">org.h2.Driver</property>
    <!--property name="connection.url">jdbc:h2:./test</property-->
    <property name="connection.username">user</property>
    <property name="connection.password">pass</property>
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.H2Dialect</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 -->
    <property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
Tiim
  • 181
  • 1
  • 11

2 Answers2

0

Can you post your hibernate.cfg? I suspect? you don't have a configured session context:

E.g thread

Stanton
  • 458
  • 3
  • 13
0

For Junit testcases you need to bind an opened Session to the current Thread.

SessionFactory sf = null;
Session s = null;
SessionHolder holder = null;

@Override
protected void onSetUp() throws Exception {
     System.out.println("On SetUP----");
     sf = (SessionFactory) beanFactory.getBean("sessionFactory");
     s = sf.openSession();
     TransactionSynchronizationManager.bindResource(sf, new SessionHolder(s));
} 

@Override
protected void onTearDown() throws Exception {
     System.out.println("On onTearDown----");
     // unbind and close the session.
     holder = (SessionHolder)TransactionSynchronizationManager.getResource(sf);
     s = holder.getSession();
     s.flush(); 
     TransactionSynchronizationManager.unbindResource(sf);
     SessionFactoryUtils.releaseSession(s, sf);
     // teardown code here
}

If you want to no more care about LazyInitialisationException read this, its well explain what happens, and how to resolve the problem definitely

Community
  • 1
  • 1
Nassim MOUALEK
  • 4,180
  • 3
  • 20
  • 38