rathje40
rathje40

Reputation: 35

Weird issue with Hibernate implicit join

I'm getting a strange error from Hibernate 4.1.9. Here's my entity class structure:

@Entity
@Table( name = "security_grant", catalog = "security")
public class SecurityGrantDO implements Serializable
{
    private long id;
    private SecuritySubjectDO securitySubject;
    @Id
    @GeneratedValue( strategy = IDENTITY )
    @Column( name = "id", unique = true, nullable = false )
    public long getId()
    {
        return this.id;
    }
    public void setId( long id )
    {
        this.id = id;
    }
    @Column( name = "security_subject__id", nullable = false )
    public SecuritySubjectDO getSecuritySubject()
    {
        return securitySubject;
    }
    public void setSecuritySubject( SecuritySubjectDO securitySubject )
    {
        this.securitySubject = securitySubject;
    }
}

@Entity
@Table( name = "security_subject", catalog = "security")
public class SecuritySubjectDO implements Serializable
{
    private long id;
    private ObjectType domainObjectType;
    private long domainObjectId;
    @Id
    @GeneratedValue( strategy = IDENTITY )
    @Column( name = "id", unique = true, nullable = false )
    public long getId()
    {
        return this.id;
    }
    public void setId( long id )
    {
        this.id = id;
    }
    @Column( name = "domain_object_type", nullable = false )
    @Enumerated(EnumType.STRING)
    public ObjectType getDomainObjectType()
    {
        return domainObjectType;
    }
    public void setDomainObjectType( ObjectType domainObjectType )
    {
        this.domainObjectType = domainObjectType;
    }
    @Column( name = "domain_object__id", nullable = false)
    public long getDomainObjectId()
    {
        return domainObjectId;
    }
    public void setDomainObjectId( long domainObjectId )
    {
        this.domainObjectId = domainObjectId;
    }
}

Here is my query:

Query query = session.createQuery( "from SecurityGrantDO g where g.securitySubject.domainObjectId = :doid and " +
            "g.securitySubject.domainObjectType = :dot" );

When this executes, Hibernate is throwing:

org.hibernate.QueryException: could not resolve property: domainObjectId of: com.jelli.phoenix.model.security.SecurityGrantDO [from com.jelli.phoenix.model.security.SecurityGrantDO g where g.securitySubject.domainObjectId = :doid and g.securitySubject.domainObjectType = :dot]

Huh? domainObjectId isn't a property of SecurityGrantDO; it's a property of SecuritySubjectDO. I figure the message itself is probably just a bug, but why is the implicit join failing?

Upvotes: 0

Views: 257

Answers (1)

Mikko Maunu
Mikko Maunu

Reputation: 42114

There is no relation mapped from SecurityGrantDO to the SecuritySubjectDO. With following mapping:

@Column( name = "security_subject__id", nullable = false )
public SecuritySubjectDO getSecuritySubject()

Hibernate tries to treat it as a Serializable persistent attribute ofSecurityGrantDO and gets confused. If relation between these two entities is needed, following is way to go:

@ManyToOne //or @OneToOne, depends about preferred domain model
@JoinColumn( name = "security_subject__id", nullable = false )
public SecuritySubjectDO getSecuritySubject() {
    return securitySubject;
}

Upvotes: 2

Related Questions