Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] question about one-to-many

JPA 1.0 does not allow a unidirectional OneToMany without a join table.  In
general it is best to define a ManyToOne back.  JPA 2.0 will allow this, and
EclipseLink 1.1 development stream already supports this.  

In EclipseLink 1.0 you could workaround the issue using a
DescriptorCustomizer to set the relationship to a OneToManyMapping using a
target foreign key, but you will need some way of maintaining the foreign
key field in the target object.


Jan Kriesten wrote:
> 
> 
> Hi Zachary,
> 
>> I do not believe that unidirectional @OneToMany mapping without a join
>> table is supported by the JPA1.0 spec.
> 
> that's strange, EJB 3.0 suggests that it should be possible with the XML I
> posted (found that in O'Reilly EJB 3.0 5th Edition). Though I don't really
> know
> for JPA 1.0...
> 
> Best regards, --- Jan.
> 
> 
>> For JPA1.0 you either need to
>> do bidirectional as I suggested before or use a join table (which is
>> what the error is complaining about not finding)
>> 
>> Take a look at
>> http://wiki.eclipse.org/EclipseLink/Development/JPA2.0/uni-directional_onetomany_mapping
>> for
>> info on support being added to eclipselink.
>> 
>> -zach
>> 
>> On Oct 14, 2008, at 11:10 AM, Jan Kriesten wrote:
>> 
>>>
>>> Hi Zachary,
>>>
>>>> If I'm understanding your question correctly you want a bi-directional
>>>> relationship between Zutat and Rezept with Rezept being the owning side
>>>> of the relationship.
>>>
>>> no, that I figured. I want a uni-directional mapping from Rezept 1 ->
>>> 8 Zutat
>>> (using @OneToMany).
>>>
>>> Best regards, --- Jan.
>>>
>>>> Rezept needs to have:
>>>>
>>>> @OneToMany(mappedBy="rezept")
>>>> private List<Zutat> zutats;
>>>>
>>>> and Zutat needs to have:
>>>>
>>>> @ManyToOne
>>>> @JoinColumn(name="rezid" referencedColumnName="id")
>>>> private Rezept rezept;  //note that the field name is what is used in
>>>> the 'mappedBy' attribute on the owning side
>>>>
>>>> Hopefully that helps,
>>>>
>>>> -zach
>>>>
>>>> On Oct 14, 2008, at 9:59 AM, Jan Kriesten wrote:
>>>>
>>>>>
>>>>> Hi,
>>>>>
>>>>> I've an understanding problem using one-to-many:
>>>>>
>>>>> I've got the following tables:
>>>>>
>>>>> REZ_REZEPTE:
>>>>> id: int
>>>>> name: String
>>>>>
>>>>> REZ_ZUT:
>>>>> id: int
>>>>> rezid: int
>>>>> name: String
>>>>>
>>>>> Now I want to have a List of all REZ_ZUT in REZ_REZEPTE.
>>>>>
>>>>> This actually works:
>>>>>
>>>>> @Entity
>>>>> @Table { val name = "REZ_REZEPTE" }
>>>>> @serializable
>>>>> class Rezept {
>>>>> @Id @GeneratedValue
>>>>> @BeanProperty var id: Int = _id
>>>>> @BeanProperty var name: String = _name
>>>>> @OneToMany { val cascade = Array( CascadeType.ALL ), val targetEntity
>>>>> =
>>>>> classOf[Zutat], val mappedBy = "rezept" }
>>>>> @BeanProperty var zutaten: JList[Zutat] = null
>>>>> }
>>>>>
>>>>> But this needs class Zutat mapping back to 'Rezept'.
>>>>>
>>>>> It should work to use
>>>>>
>>>>> @OneToMany { val cascade = Array( CascadeType.ALL ), val targetEntity
>>>>> =
>>>>> classOf[Zutat], val fetch=FetchType.EAGER }
>>>>> @JoinColumn { val name = "rezid" }
>>>>> @BeanProperty var zutaten: JList[Zutat] = null
>>>>>
>>>>> but that give an error:
>>>>>
>>>>> Internal Exception: Exception [EclipseLink-7160] (Eclipse Persistence
>>>>> Services -
>>>>> 1.0.1 (Build 20080905)):
>>>>> org.eclipse.persistence.exceptions.ValidationException
>>>>> Exception Description: @OneToMany for attribute name [zutaten] in
>>>>> entity class
>>>>> [class Rezept] 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.
>>>>>
>>>>> I tried to use the xml-variant instead (which at least allows to
>>>>> define
>>>>> join-columns):
>>>>>
>>>>>  <entity class="Rezept">
>>>>>    <attributes>
>>>>>      <id name="id"><generated-value /></id>
>>>>>      <one-to-many name="zutaten" target-entity="Zutat" fetch="EAGER">
>>>>>        <join-column name="rezid"/>
>>>>>      </one-to-many>
>>>>>    </attributes>
>>>>>  </entity>
>>>>>
>>>>> But that gives a SQL-Exception - and I really don't know why
>>>>> EclipseLink is
>>>>> trying to access a table named "rez_rezepte_rez_zut":
>>>>>
>>>>> Exception [EclipseLink-4002] (Eclipse Persistence Services - 1.0.1
>>>>> (Build
>>>>> 20080905)): org.eclipse.persistence.exceptions.DatabaseException
>>>>> Internal Exception:
>>>>> com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table
>>>>> 'rez_rezepte_rez_zut' doesn't exist
>>>>> Error Code: 1146
>>>>> Call: SELECT t1.ID, t1.EINHEIT, t1.MENGE_MAX, t1.MENGE_MIN,
>>>>> t1.MENGE_ORG,
>>>>> t1.NAME, t1.REZID FROM REZ_REZEPTE_REZ_ZUT t0, REZ_ZUT t1 WHERE
>>>>> ((t0.Rezept_ID =
>>>>> ?) AND (t1.ID = t0.zutaten_ID))
>>>>>
>>>>> Can someone give me some insight where I'm going wrong?
>>>>>
>>>>> Best regards, --- Jan.
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/question-about-one-to-many-tp19977786p19994581.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top