Hibernate version: Hibernate Annotations 3.3.1.GA, Hibernate 3.2.6, Hibernate EntityManager 3.3.2.GA
Hi, all.
I have a one-to-many that appears to be by-the-book according to the hibernate documentation, but the collection is always empty. If anyone has any ideas, I'd sure appreciate them.
The two classes:
Code:
package com.foo.client.dataobject;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
/**
* Data object that contains information about one of our clients.
*/
@Entity
@Table(name = "client")
public class Client {
@Id
@Column(name = "id")
@Type(type = "com.foo.dbspecific.UUIDType")
@GeneratedValue(generator = "inf-uuid")
@GenericGenerator(name = "inf-uuid", strategy = "com.foo.dbspecific.UUIDGenerator")
protected UUID mId;
@OneToMany(mappedBy = "mClient")
private final Set<UserLogin> mUsers = new HashSet<UserLogin>();
// Some other columns....
/**
* Default constructor for JPA.
*/
Client() {
}
// Other methods...
}
@Entity
@Table(name = "user_login")
public class UserLogin {
@Id
@Column(name = "id")
@Type(type = "com.foo.dbspecific.UUIDType")
@GeneratedValue(generator = "inf-uuid")
@GenericGenerator(name = "inf-uuid", strategy = "com.foo.dbspecific.UUIDGenerator")
protected UUID mId;
@ManyToOne
@JoinColumn(name = "client")
private Client mClient;
public UserLogin(String login, String name, String unencryptedPassword, Client client) {
mClient = client;
// ... Other setters
}
// ... other columns, methods, etc.
The user_login table has a "client" column that is a FK to the client id column.
The test:
Code:
public class UserLoginDaoTest extends AbstractTransactionalTestNGSpringContextTests {
@PersistenceContext
private EntityManager em;
@Test(groups = { "functional" })
public void createSaveFindBug() {
Client c = (Client)em.createQuery("from Client c where c.mName=:name").setParameter("name", "test1").getSingleResult();
System.out.println("Size 1: " + c.getUserLogins().size());
UserLogin ul = (UserLogin)em.createQuery("from UserLogin u where u.mClient.mId=:id").setParameter("id", c.getId()).getSingleResult();
System.out.println("UserLogin is " + ul);
UserLogin u2 = new UserLogin("login 2", "login 2 name", "mypassword2", c);
em.persist(u2);
em.flush();
// Refresh client, so that its user logins get repopulated.
em.refresh(c);
System.out.println("Size 2: " + c.getUserLogins().size());
}
}
The output:
Code:
14:12:40.836 [1] DEBUG o.s.o.j.JpaTransactionManager: Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@6b51d8] for JPA transaction
Size 1: 0
UserLogin is com.foo.client.dataobject.UserLogin@a1c582
Size 2: 0
So, the test looks up an existing client that should have one UserLogin associated with it. However, when it prints out the size of the set, it's empty. The UserLogin exists correctly b/c the following hibernate query looks up the UserLogin whose mClient is equal to the client read in, and finds it. Then, just to add more surety to it, a new UserLogin for that Client is created and saved to the db, then the Client refreshed, and still its collection size is 0. As evidenced by the log below, which is from the same test, but with TRACE level logging for hibernate binding, it appears that Hibernate is actually finding the right UserLogin(s) from the db, but it just fails to put them in the collection appropriately:
Code:
14:46:11.057 [1] DEBUG o.h.SQL: select client0_.id as id0_, client0_.database_user as database2_0_, client0_.insert_tm as insert3_0_, client0_.last_update_tm as last4_0_, client0_.name as name0_, client0_.status as status0_ from client client0_ where client0_.name=?
14:46:11.057 [1] TRACE o.h.t.StringType: binding 'test1' to parameter: 1
14:46:11.260 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_
14:46:11.260 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_
14:46:11.260 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_
14:46:11.260 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_
14:46:11.260 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_
14:46:11.494 [1] DEBUG o.h.SQL: select musers0_.client as client1_, musers0_.id as id1_, musers0_.id as id2_0_, musers0_.client as client2_0_, musers0_.encrypted_password as encrypted2_2_0_, musers0_.insert_tm as insert3_2_0_, musers0_.last_update_tm as last4_2_0_, musers0_.login as login2_0_, musers0_.name as name2_0_, musers0_.status as status2_0_ from user_login musers0_ where musers0_.client=?
14:46:11.713 [1] TRACE o.h.t.StringType: returning 'asdf' as column: encrypted2_2_0_
14:46:11.713 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_2_0_
14:46:11.713 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_2_0_
14:46:11.713 [1] TRACE o.h.t.StringType: returning 'greg login' as column: login2_0_
14:46:11.713 [1] TRACE o.h.t.StringType: returning 'greg' as column: name2_0_
14:46:11.713 [1] DEBUG o.h.t.EnumType: Returning '0' as column status2_0_
14:46:11.713 [1] DEBUG o.h.SQL: select userlogin0_.id as id2_1_, userlogin0_.client as client2_1_, userlogin0_.encrypted_password as encrypted2_2_1_, userlogin0_.insert_tm as insert3_2_1_, userlogin0_.last_update_tm as last4_2_1_, userlogin0_.login as login2_1_, userlogin0_.name as name2_1_, userlogin0_.status as status2_1_, client1_.id as id0_0_, client1_.database_user as database2_0_0_, client1_.insert_tm as insert3_0_0_, client1_.last_update_tm as last4_0_0_, client1_.name as name0_0_, client1_.status as status0_0_ from user_login userlogin0_ left outer join client client1_ on userlogin0_.client=client1_.id where userlogin0_.id=?
14:46:11.729 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:11.729 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:11.729 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:11.729 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:11.729 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
14:46:11.729 [1] TRACE o.h.t.StringType: returning 'asdf' as column: encrypted2_2_1_
14:46:11.729 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_2_1_
14:46:11.729 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_2_1_
14:46:11.729 [1] TRACE o.h.t.StringType: returning 'greg login' as column: login2_1_
14:46:11.729 [1] TRACE o.h.t.StringType: returning 'greg' as column: name2_1_
14:46:11.729 [1] DEBUG o.h.t.EnumType: Returning '0' as column status2_1_
14:46:11.729 [1] DEBUG o.h.SQL: select client0_.id as id0_0_, client0_.database_user as database2_0_0_, client0_.insert_tm as insert3_0_0_, client0_.last_update_tm as last4_0_0_, client0_.name as name0_0_, client0_.status as status0_0_ from client client0_ where client0_.id=?
14:46:11.729 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:11.729 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:11.729 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:11.729 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:11.729 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
14:46:11.729 [1] DEBUG o.h.SQL: select client0_.id as id0_0_, client0_.database_user as database2_0_0_, client0_.insert_tm as insert3_0_0_, client0_.last_update_tm as last4_0_0_, client0_.name as name0_0_, client0_.status as status0_0_ from client client0_ where client0_.id=?
14:46:11.744 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:11.744 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:11.744 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:11.744 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:11.744 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
Size 1: 0
14:46:11.901 [1] DEBUG o.h.SQL: select userlogin0_.id as id2_, userlogin0_.client as client2_, userlogin0_.encrypted_password as encrypted2_2_, userlogin0_.insert_tm as insert3_2_, userlogin0_.last_update_tm as last4_2_, userlogin0_.login as login2_, userlogin0_.name as name2_, userlogin0_.status as status2_ from user_login userlogin0_ where userlogin0_.client=?
14:46:11.901 [1] TRACE o.h.t.StringType: returning 'asdf' as column: encrypted2_2_
14:46:11.901 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_2_
14:46:11.901 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_2_
14:46:11.901 [1] TRACE o.h.t.StringType: returning 'greg login' as column: login2_
14:46:11.901 [1] TRACE o.h.t.StringType: returning 'greg' as column: name2_
14:46:11.901 [1] DEBUG o.h.t.EnumType: Returning '0' as column status2_
14:46:11.901 [1] DEBUG o.h.SQL: select client0_.id as id0_0_, client0_.database_user as database2_0_0_, client0_.insert_tm as insert3_0_0_, client0_.last_update_tm as last4_0_0_, client0_.name as name0_0_, client0_.status as status0_0_ from client client0_ where client0_.id=?
14:46:11.916 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:11.916 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:11.916 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:11.916 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:11.916 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
UserLogin is com.foo.client.dataobject.UserLogin@749436
14:46:12.073 [1] DEBUG o.h.SQL: insert into user_login (client, encrypted_password, insert_tm, last_update_tm, login, name, status, id) values (?, ?, ?, ?, ?, ?, ?, ?)
14:46:12.073 [1] TRACE o.h.t.StringType: binding 'h2en0xataMtgfHyAW4Wf+ngnfdoTt6Pi6LU8rTyrvG4=' to parameter: 2
14:46:12.073 [1] TRACE o.h.t.TimestampType: binding '2008-08-20 14:46:11' to parameter: 3
14:46:12.073 [1] TRACE o.h.t.TimestampType: binding '2008-08-20 14:46:11' to parameter: 4
14:46:12.073 [1] TRACE o.h.t.StringType: binding 'login 2' to parameter: 5
14:46:12.073 [1] TRACE o.h.t.StringType: binding 'login 2 name' to parameter: 6
14:46:12.073 [1] DEBUG o.h.t.EnumType: Binding '0' to parameter: 7
14:46:12.104 [1] DEBUG o.h.SQL: select client0_.id as id0_0_, client0_.database_user as database2_0_0_, client0_.insert_tm as insert3_0_0_, client0_.last_update_tm as last4_0_0_, client0_.name as name0_0_, client0_.status as status0_0_ from client client0_ where client0_.id=?
14:46:12.119 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:12.119 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:12.119 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:12.119 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:12.119 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
14:46:12.119 [1] DEBUG o.h.SQL: select musers0_.client as client1_, musers0_.id as id1_, musers0_.id as id2_0_, musers0_.client as client2_0_, musers0_.encrypted_password as encrypted2_2_0_, musers0_.insert_tm as insert3_2_0_, musers0_.last_update_tm as last4_2_0_, musers0_.login as login2_0_, musers0_.name as name2_0_, musers0_.status as status2_0_ from user_login musers0_ where musers0_.client=?
14:46:12.119 [1] TRACE o.h.t.StringType: returning 'h2en0xataMtgfHyAW4Wf+ngnfdoTt6Pi6LU8rTyrvG4=' as column: encrypted2_2_0_
14:46:12.119 [1] TRACE o.h.t.TimestampType: returning '2008-08-20 14:46:11' as column: insert3_2_0_
14:46:12.119 [1] TRACE o.h.t.TimestampType: returning '2008-08-20 14:46:11' as column: last4_2_0_
14:46:12.119 [1] TRACE o.h.t.StringType: returning 'login 2' as column: login2_0_
14:46:12.119 [1] TRACE o.h.t.StringType: returning 'login 2 name' as column: name2_0_
14:46:12.119 [1] DEBUG o.h.t.EnumType: Returning '0' as column status2_0_
14:46:12.119 [1] DEBUG o.h.SQL: select userlogin0_.id as id2_1_, userlogin0_.client as client2_1_, userlogin0_.encrypted_password as encrypted2_2_1_, userlogin0_.insert_tm as insert3_2_1_, userlogin0_.last_update_tm as last4_2_1_, userlogin0_.login as login2_1_, userlogin0_.name as name2_1_, userlogin0_.status as status2_1_, client1_.id as id0_0_, client1_.database_user as database2_0_0_, client1_.insert_tm as insert3_0_0_, client1_.last_update_tm as last4_0_0_, client1_.name as name0_0_, client1_.status as status0_0_ from user_login userlogin0_ left outer join client client1_ on userlogin0_.client=client1_.id where userlogin0_.id=?
14:46:12.119 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:12.119 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:12.151 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:12.151 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:12.151 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
14:46:12.151 [1] TRACE o.h.t.StringType: returning 'h2en0xataMtgfHyAW4Wf+ngnfdoTt6Pi6LU8rTyrvG4=' as column: encrypted2_2_1_
14:46:12.166 [1] TRACE o.h.t.TimestampType: returning '2008-08-20 14:46:11' as column: insert3_2_1_
14:46:12.166 [1] TRACE o.h.t.TimestampType: returning '2008-08-20 14:46:11' as column: last4_2_1_
14:46:12.166 [1] TRACE o.h.t.StringType: returning 'login 2' as column: login2_1_
14:46:12.166 [1] TRACE o.h.t.StringType: returning 'login 2 name' as column: name2_1_
14:46:12.166 [1] DEBUG o.h.t.EnumType: Returning '0' as column status2_1_
14:46:12.166 [1] DEBUG o.h.SQL: select client0_.id as id0_0_, client0_.database_user as database2_0_0_, client0_.insert_tm as insert3_0_0_, client0_.last_update_tm as last4_0_0_, client0_.name as name0_0_, client0_.status as status0_0_ from client client0_ where client0_.id=?
14:46:12.166 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:12.166 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:12.166 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:12.166 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:12.166 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
14:46:12.182 [1] TRACE o.h.t.StringType: returning 'asdf' as column: encrypted2_2_0_
14:46:12.229 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_2_0_
14:46:12.229 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_2_0_
14:46:12.229 [1] TRACE o.h.t.StringType: returning 'greg login' as column: login2_0_
14:46:12.229 [1] TRACE o.h.t.StringType: returning 'greg' as column: name2_0_
14:46:12.229 [1] DEBUG o.h.t.EnumType: Returning '0' as column status2_0_
14:46:12.229 [1] DEBUG o.h.SQL: select userlogin0_.id as id2_1_, userlogin0_.client as client2_1_, userlogin0_.encrypted_password as encrypted2_2_1_, userlogin0_.insert_tm as insert3_2_1_, userlogin0_.last_update_tm as last4_2_1_, userlogin0_.login as login2_1_, userlogin0_.name as name2_1_, userlogin0_.status as status2_1_, client1_.id as id0_0_, client1_.database_user as database2_0_0_, client1_.insert_tm as insert3_0_0_, client1_.last_update_tm as last4_0_0_, client1_.name as name0_0_, client1_.status as status0_0_ from user_login userlogin0_ left outer join client client1_ on userlogin0_.client=client1_.id where userlogin0_.id=?
14:46:12.244 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:12.244 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:12.244 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:12.244 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:12.244 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
14:46:12.244 [1] TRACE o.h.t.StringType: returning 'asdf' as column: encrypted2_2_1_
14:46:12.244 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_2_1_
14:46:12.244 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_2_1_
14:46:12.244 [1] TRACE o.h.t.StringType: returning 'greg login' as column: login2_1_
14:46:12.244 [1] TRACE o.h.t.StringType: returning 'greg' as column: name2_1_
14:46:12.244 [1] DEBUG o.h.t.EnumType: Returning '0' as column status2_1_
14:46:12.244 [1] DEBUG o.h.SQL: select client0_.id as id0_0_, client0_.database_user as database2_0_0_, client0_.insert_tm as insert3_0_0_, client0_.last_update_tm as last4_0_0_, client0_.name as name0_0_, client0_.status as status0_0_ from client client0_ where client0_.id=?
14:46:12.260 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:12.260 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:12.260 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:12.260 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:12.260 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
14:46:12.260 [1] DEBUG o.h.SQL: select client0_.id as id0_0_, client0_.database_user as database2_0_0_, client0_.insert_tm as insert3_0_0_, client0_.last_update_tm as last4_0_0_, client0_.name as name0_0_, client0_.status as status0_0_ from client client0_ where client0_.id=?
14:46:12.260 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:12.260 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:12.260 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:12.260 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:12.260 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
14:46:12.260 [1] DEBUG o.h.SQL: select client0_.id as id0_0_, client0_.database_user as database2_0_0_, client0_.insert_tm as insert3_0_0_, client0_.last_update_tm as last4_0_0_, client0_.name as name0_0_, client0_.status as status0_0_ from client client0_ where client0_.id=?
14:46:12.276 [1] TRACE o.h.t.StringType: returning 'test1' as column: database2_0_0_
14:46:12.276 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: insert3_0_0_
14:46:12.276 [1] TRACE o.h.t.TimestampType: returning '2008-07-30 15:28:53' as column: last4_0_0_
14:46:12.276 [1] TRACE o.h.t.StringType: returning 'test1' as column: name0_0_
14:46:12.276 [1] DEBUG o.h.t.EnumType: Returning '0' as column status0_0_
Size 2: 0
I'd appreciate it greatly if anyone can see if I'm doing something wrong -- otherwise, I assume it's a hibernate bug, but it seems like such a simple thing that it's hard to imagine the bug hasn't been found previously....
Thanks much in advance,
Greg