persistence@glassfish.java.net

Re: ManyToMany with list of an utility class which contains the other objekt fur mapping and an additional attribute

From: James Sutherland <jamesssss_at_yahoo.com>
Date: Thu, 20 Sep 2007 06:54:45 -0700 (PDT)

A component in Hibernate is just a different term for an Embeddable in JPA
(and an Aggregate in TopLink Essentials). You are still mapping the object,
just as an Embeddable instead of as an Entity. This is NOT something that
only exists in Hibernate.

TopLink Essentials supports the SAME functionality of the components in
Hibernate that Marina was referring to. TopLink supports these through both
JPA Embeddables and through Aggregate and AggregateCollection descriptors in
the TopLink Essentials API. Although the JPA specification does not allow
Embeddables to have relationships, TopLink Essentials does support this
through its' API for Aggregate and AggregateCollection descriptors. You can
define a collection of embedded objects in TopLink Essentials through using
a DescriptorCustomizer and the descriptor code API, refer to
AggregateCollectionMapping, the aggregate's descriptor will also have to be
defined and an AggregateCollection descriptor.

However I don't think your model should use these, regardless of what
product you are using. As Gordon has stated you should define your
ShoppingCartPropertiesDTO WITH the @Entity annotation, it should define a
@ManyToOne to BOTH CustomerDTO and ProductDTO, and CustomerDTO should define
a @OneToMany to it. For its id (primary key) you can either add a generated
id field (recommended) or add additional @Id fields in the class for
customerId and productId. Technically you could use the @ManyToOne's as the
primary key and TopLink Essentials does support this, just not through the
JPA API, so you need to duplicate the fields unless you add a generated id
or use a DescriptorCustomizer. This is a very common pattern in ORM and
typically a better solution than using an AggregateCollectionMapping as it
allows the ShoppingCartPropertiesDTO to be treated like a first class
citizen.

---
http://wiki.java.net/bin/view/People/JamesSutherland James Sutherland 
Marina Vatkina wrote:
> 
> I couldn't find much googling for an answer.
> 
> Hibernate supports it in a way similar to JPA but it doesn't require you
> to have 
> an extra entity:
> http://www.hibernate.org/hib_docs/reference/en/html/components.html
> 
> HTH,
> -marina
> 
> PLC_David wrote:
>> Can you tell me which provider can do this ???
>> 
>> 
>> David
>> 
>> 
>> 
>> Marina Vatkina wrote:
>> 
>>>There is no portable way to support ManyToMany relationships with an
extra 
>>>attribute. Some providers can do it in their own way. For a portable
>>>solution, 
>>>you need to map them as 2 OneToMany relationships with an Entity that
>>>represents 
>>>that extra attribute.
>>>
>>>Regards,
>>>-marina
>>>
>>>PLC_David wrote:
>>>
>>>>Hello Gordon, 
>>>>
>>>>I don't understand it :confused:. I've deleted @Entity in the
>>>>ShoppingCartPropertiesDTO, but how is the @manytomany   
>>>>in CustomerDTO ?? 
>>>>
>>>>If I have the ShoppingCartPropertiesDTO without the @Entity than I get
>>>>following exception:
>>>>
>>>>Exception Description: Missing descriptor for
>>>>[ShoppingCartPropertiesDTO]. 
>>>>Verify that the descriptor has been properly registered with the
Session.
>>>>
>>>>Could you write what you mean, please ? 
>>>>
>>>>
>>>>
>>>>Gordon Yorke wrote:
>>>>
>>>>
>>>>>Hello,
>>>>>  Yes you can map the ShoppingCartPropertiesDTO using the @ManyToMany
>>>>>annotation.  The ShoppingCartPropertiesDTO will have to be marked as an
>>>>>Entity.  Not marking the ShoppingCartPropertiesDTO as an Entity is most
>>>>>likely the cause of the exception you are seeing.
>>>>>--Gordon
>>>>>
>>>>>-----Original Message-----
>>>>>From: PLC_David [mailto:spaceman2k4_at_gmx.de]
>>>>>Sent: Monday, September 17, 2007 7:12 PM
>>>>>To: persistence_at_glassfish.dev.java.net
>>>>>Subject: ManyToMany with list of an utility class which contains the
>>>>>other objekt fur mapping and an additional attribute
>>>>>
>>>>>
>>>>>
>>>>>Hallo, I work with JPA for the first time.
>>>>>
>>>>>My database:
>>>>>
>>>>>Customer :  ID | NAME | other columns
>>>>>Product : ID | DISCRIMINATOR |  NAME |other columns |
HARDWARE_INTERFACE
>> 
>> |
>> 
>>>>>XML_ID
>>>>>Customer_Product_Rel:  CUSTOMER_ID | PRODUCT_ID | AMOUNT
>>>>>
>>>>>My objects:
>>>>>
>>>>>@Entity
>>>>>CustomerDTO:  attributes like id, name ... and
>>>>>List<ShoppingCartPropertiesDTO> products = new
>>>>>ArrayList<ShoppingCartPropertiesDTO>();
>>>>>
>>>>>ShoppingCartPropertiesDTO: private ProductDTO product = null;	private
>>>>>Integer amount = null;
>>>>>
>>>>>@Entity
>>>>>@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
>>>>>@DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType =
>>>>>DiscriminatorType.STRING, length = 2)
>>>>>abstract class Product:  id, name ,descr, ...
>>>>>
>>>>>@Entity(name="SoftwareDTO")
>>>>>@DiscriminatorValue("SW")
>>>>>public class SoftwareDTO extends ProductDTO: private
>> 
>> SystemRequirementsDTO
>> 
>>>>>systemRequirements = null;	private String language = null;
>>>>>
>>>>>
>>>>>My question: Can I map the customer with a list of the additional
object
>>>>>(ShoppingCartPropertiesDTO) which contains the product and the amount?
>> 
>> And
>> 
>>>>>how is the ManyToMany annotation ?
>>>>>
>>>>>
>>>>>
>>>>>An additional question: What means the exception with missing
descriptor
>>>>>??
>>>>>
>>>>>Exception Description: Missing descriptor for
>> 
>> [ShoppingCartPropertiesDTO].
>> 
>>>>>Verify that the descriptor has been properly registered with the
Session.
>>>>>	at
>>>>>oracle.toplink.essentials.internal.ejb.cmp3.transaction.base.EntityTransactionImpl.commit(EntityTransactionImpl.java:120)
>>>>>	at
>>>>>oracle.toplink.essentials.internal.ejb.cmp3.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:60)
>>>>>	at test.JPATester.runTest2(JPATester.java:186)
>>>>>Caused by: Exception [TOPLINK-7009] (Oracle TopLink Essentials - 2.0
>>>>>(Build
>>>>>b58g-fcs (09/07/2007))):
>>>>>oracle.toplink.essentials.exceptions.ValidationException
>>>>>
>>>>>
>>>>>
>>>>>Thanks for all posts
>>>>>David
>>>>>
>>>>>
>>>>>
>>>>>--
>>>>>View this message in context:
>>>>>http://www.nabble.com/ManyToMany-with-list-of-an-utility-class-which-contains-the-other-objekt-f%C3%BCr-mapping-and-an-additional-attribute-tf4470610.html#a12746960
>>>>>Sent from the java.net - glassfish persistence mailing list archive at
>>>>>Nabble.com.
>>>>>
>>>>>
>>>>
>>>>
>>>
>> 
> 
> 
-- 
View this message in context: http://www.nabble.com/ManyToMany-with-list-of-an-utility-class-which-contains-the-other-object-for-mapping-and-an-additional-attribute-tf4470610.html#a12797720
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.