Eager loading @OneToMany class with @EmbeddedId and @DiscriminatorColumn fails
------------------------------------------------------------------------------
Key: HHH-7267
URL:
https://hibernate.onjira.com/browse/HHH-7267
Project: Hibernate ORM
Issue Type: Bug
Components: core
Affects Versions: 4.1.2, 3.6.10
Environment: Windows 7 x64
Reporter: Jan Čustović
Priority: Critical
When I try to load a user which has an owner I get this error. The problem is in
@OneToMany userRoles which has @EmbeddedId in combination with @DiscriminatorColumn (if I
remove @DiscriminatorColumn it works). Hibernate gets confused because users and users
owner userRoles must be loaded.
{code:title=Error|borderStyle=solid}
13:00:08.925 [http-bio-8080-exec-1] ERROR org.hibernate.AssertionFailure - HHH000099: an
assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to
unsafe use of the session): org.hibernate.AssertionFailure: null identifier
13:00:08.951 [http-bio-8080-exec-1] ERROR
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor - null identifier
org.hibernate.AssertionFailure: null identifier
at org.hibernate.engine.spi.EntityKey.<init>(EntityKey.java:69)
~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
at
org.hibernate.internal.AbstractSessionImpl.generateEntityKey(AbstractSessionImpl.java:242)
~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:722)
~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:635)
~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:850)
~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]
...
{code}
{code:title=User.java|borderStyle=solid}
@Entity
@Table(name = "user")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User extends AbstractBaseEntity {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "owner_id", nullable = true)
private User owner;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.user")
private Set<UserRole> userRoles;
... Other fields, getters & setters, hash, equals
}
{code}
{code:title=UserRole.java|borderStyle=solid}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType =
DiscriminatorType.STRING)
@DiscriminatorValue("DEFAULT")
@Table(name = "user_role")
public class UserRole implements Serializable {
@EmbeddedId
private UserRolePK pk;
... Getters & setters, hash, equals
}
{code}
{code:title=UserRolePK.java|borderStyle=solid}
@Embeddable
public class UserRolePK implements Serializable {
@ManyToOne
private User user;
@ManyToOne
private Role role;
... Getters & setters, hash, equals
}
{code}
When I try to change owner to lazy load getOwner() return object with all properties
null.
{code}
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "owner_id", nullable = true)
private User owner;
...
{code}
Changing userRoles to lazy load fixes the problem and everything works good but I need
eager load so i added FetchMode.SUBSELECT which also works but I don't want to user
hibernate annotations.
{code}
...
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.user")
@Fetch(value = FetchMode.SUBSELECT)
private Set<UserRole> userRoles;
...
{code}
--
This message is automatically generated by JIRA.
For more information on JIRA, see:
http://www.atlassian.com/software/jira