persistence@glassfish.java.net

Re: many-to-many self-reference

From: Markus Fuchs <Markus.Fuchs_at_Sun.COM>
Date: Thu, 06 Sep 2007 17:20:58 -0700

Hi Greg,

how exactly does your test set the relationships? I.e.

toy_aircraft.getParentCategories().add(toy_vehicles); // inverse side
omitted
toy_planes.getParentCategories().add(toy_aircraft); // inverse side omitted

Should result in the right relationships. You might have to swap the
joinColumns and inverseJoinColumns...

Thanks,

-- markus.

Greg Ederer wrote:
> Hi Markus and Marina,
>
> I now have:
>
> @ManyToMany
> @JoinTable(
> name="parent_child_category",
> joinColumns=_at_JoinColumn(name="parent_id",
> referencedColumnName="id"),
> inverseJoinColumns=_at_JoinColumn(name="child_id",
> referencedColumnName="id")
> )
> private List<ToyCategory> parentCategories = new
> ArrayList<ToyCategory>();
>
> @ManyToMany(mappedBy = "parentCategories")
> private List<ToyCategory> childCategories = new
> ArrayList<ToyCategory>();
>
> Now, with test data:
>
> <table name="toy_category">
> <column>id</column>
> <column>name</column>
> <column>description</column>
> <row>
> <value>1</value>
> <value><![CDATA[Toy Vehicles]]></value>
> <value><![CDATA[Toy Vehicles]]></value>
> </row>
> <row>
> <value>2</value>
> <value><![CDATA[Toy Aircraft]]></value>
> <value><![CDATA[Toy Aircraft]]></value>
> </row>
> <row>
> <value>3</value>
> <value><![CDATA[Toy Planes]]></value>
> <value><![CDATA[Toy Planes]]></value>
> </row>
> </table>
> <table name="parent_child_category">
> <column>parent_id</column>
> <column>child_id</column>
> <row>
> <value description="parent_id">1</value>
> <value description="child_id">2</value>
> </row>
> <row>
> <value description="parent_id">2</value>
> <value description="child_id">3</value>
> </row>
> </table>
>
> my tests give me:
>
> INFO - ToyCategoryDaoTest.testGetToyCategory(20) | Got ToyCategory:
> Toy Aircraft
> INFO - ToyCategoryDaoTest.testGetToyCategory(27) | Parents: Toy
> Planes |
> INFO - ToyCategoryDaoTest.testGetToyCategory(34) | Children: Toy
> Vehicles |
>
> My parents and children seem to be swapped. Not sure what I'm doing
> wrong.
>
> Thanks!
>
> Greg
>
> Markus Fuchs wrote:
>> Hi Greg,
>>
>> please try this:
>>
>> @Entity
>> public class Category
>> {
>> @Id
>> @Column(name="CATEGORY_ID")
>> private Long id;
>>
>> protected String name;
>>
>> @ManyToMany
>> @JoinTable(
>> name="PARENT_CHILD_CATEGORY",
>> joinColumns=_at_JoinColumn(name="PARENT_ID",
>> referencedColumnName="CATEGORY_ID")
>> inverseJoinColumns=_at_JoinColumn(name="CHILD_ID",
>> referencedColumnName="CATEGORY_ID")
>> )
>> protected List<Category> parentCategories = new ArrayList<Category>();
>>
>> @ManyToMany(mappedBy="parentCategories")
>> protected List<Category> childCategories = new ArrayList<Category>();
>>
>> ...
>> }
>>
>> Thanks,
>>
>> -- markus.
>>
>> Greg Ederer wrote:
>>> Hi,
>>>
>>> I have a Category class. According to my model, a Category can have
>>> many parent Categorys and many child Categorys. I'm having trouble
>>> getting the JPA annotations right for this relationship. I'm sure
>>> that this must be trivially easy.
>>>
>>> The source might something look like:
>>>
>>> @Entity
>>> public class Category
>>> {
>>> @Id
>>> private Long id;
>>>
>>> protected String name;
>>>
>>> @???
>>> protected List<Category> parentCategories = new
>>> ArrayList<Category>();
>>>
>>> @???
>>> protected List<Category> childCategories = new ArrayList<Category>();
>>>
>>> ...
>>> }
>>>
>>> What is the correct way to do this?
>>>
>>> Thanks!
>>>
>>> Greg
>>>
>
>