This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes OneToMany Unidirectional strange behavior ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "OneToMany Unidirectional strange behavior ? " Watch "OneToMany Unidirectional strange behavior ? " New topic
Author

OneToMany Unidirectional strange behavior ?

Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
I am implementing hibernate with JPA annotations and trying to do a very simple thing but seeing very different behavior in oracleAS.

I have an Order object which has many OrderLineItems and i want a unidirectional relationship NOT A BIDIRECTIONAL.

When i try the following code with a main method, it does work perfectly fine, but the same code when deploying to Oracle Application server in a session bean it fails with the following exception

Exception Description: @OneToMany for attribute name [orderLineItemsList] in entity class [class com.thoughtclicks.domains.Orders] should not have @JoinColumn(s) specified. In the case where the @OneToMany is not mapped by another entity (that is, it is the owning side and is uni-directional), it should specify (optional through defaulting) a @JoinTable.

Below is the defination of Order Object: and as it is unidirection there is no code for order in orderlineitems

@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinColumn(name="ORD_ID")
public List<OrderLineItems> getOrderLineItemsList() {
return orderLineItemsList;
}

Does anybody know what can be the issue with the oracle as deployment ?

Message was edited by:
rahul_juneja


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
Max Vandenburg
Ranch Hand

Joined: Mar 09, 2006
Posts: 51
Have a look here...
JSR220


2.1.8.5.1 Unidirectional OneToMany Relationships
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
There is a join table that is named A_B (owner name first). This join table has two foreign key
columns. One foreign key column refers to table A and has the same type as the primary key of
table A. The name of this foreign key column is formed as the concatenation of the following:
the name of entity A; "_"; the name of the primary key column in table A. The other foreign
key column refers to table B and has the same type as the primary key of table B and there is a
unique key constraint on it. The name of this foreign key column is formed as the concatenation
of the following: the name of the relationship property or field of entity A; "_"; the name
of the primary key column in table B.
Example:
@Entity
public class Employee {
private Collection<AnnualReview> annualReviews;
@OneToMany
public Collection<AnnualReview> getAnnualReviews() {
return annualReviews;
}
public void setAnnualReviews(Collection<AnnualReview> annualReviews)
{
this.annualReviews = annualReviews;
}
...
}
@Entity
public class AnnualReview {
...
}
In this example:
Requirements on the Entity Class Enterprise JavaBeans 3.0, Final Release Entities
33 5/2/06
Sun Microsystems, Inc.
Entity Employee references a collection of Entity AnnualReview.
Entity AnnualReview does not reference Entity Employee.
Entity Employee is the owner of the relationship.
The following mapping defaults apply:
Entity Employee is mapped to a table named EMPLOYEE.
Entity AnnualReview is mapped to a table named ANNUALREVIEW.
There is a join table that is named EMPLOYEE_ANNUALREVIEW (owner name first). This
join table has two foreign key columns. One foreign key column refers to table EMPLOYEE
and has the same type as the primary key of EMPLOYEE. This foreign key column is named
EMPLOYEE_<PK of EMPLOYEE>, where <PK of EMPLOYEE> denotes the name of the primary
key column of table EMPLOYEE. The other foreign key column refers to table ANNUALREVIEW
and has the same type as the primary key of ANNUALREVIEW. This foreign key
column is named ANNUALREVIEWS_<PK of ANNUALREVIEW>, where <PK of ANNUALREVIEW>
denotes the name of the primary key column of table ANNUALREVIEW. There
is a unique key constraint on the foreign key that refers to table ANNUALREVIEW.


MV
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
But I tried deploying the same on jboss and it went through.

Thanks,
Rahul
Chris Seifert
Greenhorn

Joined: Aug 09, 2008
Posts: 9
Thanks for the reference, that helps to explain why I'm having similar issues.

I'm somewhat new to EJB. Do you have any idea why the EJB standards seek to dictate database design by forcing the user to create 3 tables to model a One to Many relationship instead of the standard 2 with a foreign key to a primary key? In the past, I have used 3 tables only to model ManyToMany relationships.

BEGIN QUOTE

2.1.8.5.1 Unidirectional OneToMany Relationships
...
2.1.8.5.1 Unidirectional OneToMany Relationships
The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
There is a join table that is named A_B (owner name first). This join table has two foreign key columns. One foreign key column refers to table A and has the same type as the primary key of table A. The name of this foreign key column is formed as the concatenation of the following:
the name of entity A; "_"; the name of the primary key column in table A. The other foreign
key column refers to table B and has the same type as the primary key of table B and there is a
unique key constraint on it. The name of this foreign key column is formed as the concatenation
of the following: the name of the relationship

ENDQUOTE
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OneToMany Unidirectional strange behavior ?
 
Similar Threads
Hibernate 3 Annotations
JPA CascadeType enquiry
Couldn't persist OneToMany JoinColumn/JoinTable (Unidir) using TopLink JPA
EJB 3 in Action - JPA Doubt
Need help to correct @JoinColumn mapping in entity class with composite keys