Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA Questions(How and when to use JPA)
JPA Questions [message #531087] Mon, 03 May 2010 16:30 Go to next message
Larry  is currently offline Larry Friend
Messages: 30
Registered: December 2009
I have a few questions.

I am trying to figure out when and why to use JPA. I have done some tutorials and came to the following conclusions/questions.

1) JPA is great for returning entities rather than having to use JDBC to map over the fields.

2) Trying to figure out how to design entities can be really hard. You can not just take the DB table and call it an entity (you can sometimes, but not alot). I have the situation of converting over an application that has lots of joins. Now do I always have to design a relationship in an entity so that I can use it as a join? For instance, if I have entity A and entity B, and I want to get all records for A that have a join relationship with entity B. Do I have to have Entity B mapped into entity A? I would have preferred to just use a NamedQuery to get the data and join in B. But if the Entity (B) is not defined/mapped into entity A, I don't believe you can do this. Is this when you use a native query? Or is it a design flaw on my part. I can see a lot of these situations where I am querying for data with mutlitple table joins. I also see a lot of instances where I don't want to always have to create a relationship with the other entities. I could see where I have an Entity that has about 20 different joins to other entities/tables due to returning different sets of data using the same table/Entity. I don't think it would be good design to have the join tables mapped to one entity. Am I just missing something??

3) Also, I had a few problems with outer joins. When I used the following code:
select a from A a LEFT OUTER JOIN a.b b
where = and b.id2 = 1;
It worked except when there was no match on b, then it returned nothing. It should return entity A even if b was not found (our did I get the LEFT OUTER JOIN definition wrong?).

Any help would be appreciated!!!!
Re: JPA Questions [message #531951 is a reply to message #531087] Thu, 06 May 2010 15:07 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

2) Yes, designing a good object model can be difficult, as designing a good data model. The are tools such as Eclipse Dali that can generate a JPA object model from the data model, and generate relationships based on the database constraints.

Most object oriented developers would recommend an object model be developed to meet the application requirements and correctly model the business domain.

3) An outer join means the join condition will return a null row if there are no rows that meet the join condition. If you filter the joined row in the where clause, then this will be filtered.

select a from A a LEFT OUTER JOIN a.b b
where = and b.id2 = 1;

Will not return any a's where the b.id2 is not equal to 1.

If you also want a's that don't have a b you need,

select a from A a LEFT OUTER JOIN a.b b
where b.id2 is null or b.id2 = 1

select a from A a LEFT OUTER JOIN a.b b
where a.b is null or b.id2 = 1

James : Wiki : Book : Blog : Twitter
Previous Topic:Mapping two objects on same table
Next Topic:Filtering a OneToMany relationship
Goto Forum:

Current Time: Fri Apr 20 03:20:18 GMT 2018

Powered by FUDForum. Page generated in 0.03833 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software