This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes JPA Class Generation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA Class Generation" Watch "JPA Class Generation" New topic
Author

JPA Class Generation

Arockia Raj
Ranch Hand

Joined: Dec 08, 2006
Posts: 37
Hi All,
I am having following simple scenario

customer-->Account(relationship as one to many),

'CUST_ID' in Account Table represents the Customer for this account

when we generate JPA classes the Account class CUST_ID column is represented by customer Object.So to get customer id
i need to access customer object.For some cases i only need customerid.Is it possible to configure the tool to generate both
field(CUST_ID) and relationship field (Customer Object)

Generated Class
@Entity
@Table(name="ACCOUNT")
public class Account implements Serializable
{

@ManyToOne
@JoinColumn(name="CUST_ID")
private Customer customer;


Required Class
@Entity
@Table(name="ACCOUNT")
public class Account implements Serializable
{

@ManyToOne
@JoinColumn(name="CUST_ID")
private Customer customer;

@Column(name="CUST_ID")
private java.math.BigDecimal custId;



Regards
A.Raj
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

You can always do customer.getId() to get the id. Why would you have such a need ?


Thanks,
Tushar (SCJP 1.5)
Arockia Raj
Ranch Hand

Joined: Dec 08, 2006
Posts: 37
T Mishra wrote:You can always do customer.getId() to get the id. Why would you have such a need ?


Hi Mishra,
Thanks for the reply.The reason we dont want to use customer.getId(),this call will trigger a subquery to fetch all the customer details internally.Considering the performance we dont want to do this, and prefer having id attribute directly so that we can take this value alone and will call Customer object when we need the complete customer object information.

Regards
Raj
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

this call will trigger a subquery to fetch all the customer details

No, not unless you set the fetch mode to join or subquery.

As per the entity mapping definition you've mentioned below, it does not have a fetch mode. This means that the associations are loaded using proxies. This is the default fetch mode. Since CUST_ID is the identifier, only the id of the customer is loaded into the persistent context.

With your generated entity mapping, it should just work as you expect.
Arockia Raj
Ranch Hand

Joined: Dec 08, 2006
Posts: 37
Thanks a lot for explaining this....


T Mishra wrote:
this call will trigger a subquery to fetch all the customer details

No, not unless you set the fetch mode to join or subquery.

As per the entity mapping definition you've mentioned below, it does not have a fetch mode. This means that the associations are loaded using proxies. This is the default fetch mode. Since CUST_ID is the identifier, only the id of the customer is loaded into the persistent context.

With your generated entity mapping, it should just work as you expect.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JPA Class Generation