简体   繁体   中英

@OneToOne-Relation with “is not null”

I'm using JPA2, Unitils, jUnit + other stuff. My problem concerns two entities:

@Entity
public class CaseStuff implements Serializable {
....
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long caseStuffId;

@OneToOne(mappedBy = "CaseStuff")
private XCase xcase;
...
}

@Entity
public class XCase implements Serializable {
....
@OneToOne
@JoinColumn(name = "CASESTUFFID")
private CaseStuff caseStuff;
....
}

Both tables got the ID of the other table, so it could also be mapped the other way around. In a jUnit-Test, Unitils inserts one CaseStuff record with no XCase. I confirmed, that it really is null. BUT, then I use the following query:

"select s from CaseStuff s where s.xcase is null"

and it returns 0 CaseStuff objects. Doing the same with "is not null" returns the object, but when inspecting CaseStuff.xcase while debugging, it is clearly null.

Any idea, what's going awry here?

EDIT: The SQL generated by Hibernate translates to

select
    *
from
    CaseStuff casestuff0_ 
where
     (
        casestuff0_.xcaseid is null
    )

*Replaced all field names by *

EDIT2: Before I changed it to a OneToOne-Relation, it was an unneccessary ManyToOne. Previously the jUnit test had the query

"select s from CaseStuff s where not exists (select x from s.xcase x)"

This still works correctly for some reason, as if s.xcase was still a Set.

Thanks to all who try to figure it out with me.

As you defined your entities Xcase will have the foreign key

So Xcase table beside it's id will have caseStuff_id (or other name) as foreign key to the Xcase id.

Like in this image (make abstraction of the names)

在此处输入图片说明

In your case Person is Xcase and CaseStuff is PersonDetails

So probably you CaseStuff table (if it was generated by the persistence provider does not have a case id)

Please check on the db level to see how your tables are defined, structurally speaking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM