• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA : criteria query with join and entities without annotation  RSS feed

 
daniele licitra
Ranch Hand
Posts: 81
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all.

We have to entities:

This is a view builded by a complex query.

The other entity is



I need to join the tables:


If I list all the vordini record, i must read "referenza" even if there isn't a record in righeord.

There is a way to build a criteria query with join and no annotation in entities (simple field)?

If I use

what happen if i call vordine.getReferenza().getCodicereferenza(); and righeOrd record not exists?

How can i do this?
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you want is a left outer join, but as far as I know you need to have a relation for that. However, in your case, if there are records in the Vordine table with referenza values that do not reference a codicereferenza value in the RigheOrd table, you will get a runtime error if you execute that code.
 
daniele licitra
Ranch Hand
Posts: 81
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:However, in your case, if there are records in the Vordine table with referenza values that do not reference a codicereferenza value in the RigheOrd table, you will get a runtime error if you execute that code.


Thank you. I'am trying with ugly sql native query
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take care that any instances you construct from native queries are not managed. That means editing them won't cause the changes to automatically be saved.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to specify join type (OneToOne/OneToMany/ManyToOne/ManyToMany) and not just join column.

I can tell you from experience that if you have a One-to-one join and the secondary entity doesn't exist, then the corresponding Java object will be null.

Beyond that, of course, you have to allow for the usual eager and lazy fetch concerns.

There are other, more evil ways to form JPA Entity classes when compounding from joins, but let's start with something simple.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:I can tell you from experience that if you have a One-to-one join and the secondary entity doesn't exist, then the corresponding Java object will be null.

Is that also if there is a foreign key in the first entity? Because I've seen too many occurrences where an exception was thrown (I believe it was EntityNotFoundException, or possibly NoSuchEntityException).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!