Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Looking for hints to map inheritance relationships

Thanks for the reply Tom.  Here are the annotated property definitions
from the classes:

Product:
    @JoinColumn(nullable=false)
    @ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH})
    protected ProductCategory category;

ProductCategory:
    @OneToMany(mappedBy = "category",
cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH})
    private Set<Product> products;

Automobile:
    @JoinColumn(nullable=false)
    @ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH})
    private AutomobileCategory category;

AutomobileCategory:
    @OneToMany(mappedBy = "category",
cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH})
    private Set<Automobile> automobiles;


The following is the code that just persists some instances of the entities:
        entityManager.getTransaction().begin();
        AutomobileCategory automobileCategory = new
AutomobileCategory("AC_" + UUID.randomUUID().toString(), "ENAME",
"SNAME");
        for (int i = 0; i < 5; i++) {
            Automobile automobile = new Automobile("A_" +
UUID.randomUUID().toString(), automobileCategory, "ENAME", "SNAME");
            entityManager.persist(automobile);
        }
        entityManager.getTransaction().commit();

And I'm attaching the log as it's probably too big to inline.

I have a full testcase that probably illustrates more of what is going
on.  I'll attach those source files too.

Thanks again
- Phillip


On Thu, Jul 14, 2011 at 8:39 AM, Tom Ware <tom.ware@xxxxxxxxxx> wrote:
> Hi Phillip,
>
>  Can you please post the following:
>
> - examples of how the category fields are mapped in both Product and
> Automobile
> - a code snippet the shows what you are doing
> - logging of the SQL produced and the full exception trace (for logging, use
> persistence unit property eclipselink.logging.level=FINEST)
>
> -Tom
>
> Phillip Ross wrote:
>>
>> Hi all, I'm having a problem with mapping inheritance and not sure if
>> things are acting correctly or not.  Was hoping for some hints.
>>
>> The scenario I'm working with is that I have two related entity
>> superclasses mapped with an inheritance strategy or JOINED, and there
>> is a ManyToOne relationship between the two.  The subclass entities
>> subclass the superclasses and override the relationship (or at least
>> this is what is intended).  I had first assumed that when the tables
>> were created, a field would be created in both the parents table and
>> child table... but it appears that it was only created in the parent
>> table.  I figured that this was OK since there would be records in the
>> parent tables that would map related subclass instances, but when I
>> attempt to persist the subclass with the ManyToOne member attribute...
>> an exception is thrown caused by a not-null constraint violation
>> corresponding to the parent table's FK field.  As an experiment, I
>> changed the child class to populate the related property field in both
>> the superclass and itself, but I'm assuming this won't really work out
>> in the future as fetching the correct records may not work either.
>>
>> To put this in more concrete terms... say I have Product and
>> ProductCategory as superclasses.  Product has a property called
>> category which holds a ProductCategory instance.  Automobile
>> subclasses Product and AutomobileCategory subclasses ProductCategory.
>> The Automobile class defined a property named category and is defined
>> to be AutomobileCategory.  Since persisting an Automobile and an
>> AutomobileCategory would create records in all four tables... shouldnt
>> eclipselink know that the category field in the Product table relating
>> to ProductCategory be used to map the Automobile instances to the
>> AutomobileCategory instances?
>>
>> I'm hoping someone knoww if I'm trying to map a scenario that's not
>> supported or if there is a trick to doing this?  Perhaps an
>> eclipselink specific trick that goes beyond the JPA spec?  If it is
>> supported and sounds like a bug... I have a test case to submit :)
>>
>> Thanks!
>> - Phillip
>> _______________________________________________
>> eclipselink-users mailing list
>> eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>

Attachment: FINEST.log
Description: Binary data

Attachment: TestCaseMain.java
Description: Binary data

Attachment: Product.java
Description: Binary data

Attachment: ProductCategory.java
Description: Binary data

Attachment: Automobile.java
Description: Binary data

Attachment: Automobile.java
Description: Binary data

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence";
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd";
             version="2.0" >

    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
        <class>testcase1.Automobile</class>
        <class>testcase1.AutomobileCategory</class>
		<class>testcase1.Product</class>
        <class>testcase1.ProductCategory</class>
		<properties>
			<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
			<property name="javax.persistence.jdbc.url" value="jdbc:derby:testcaseDb;create=true" />
			<property name="javax.persistence.jdbc.user" value="test" />
			<property name="javax.persistence.jdbc.password" value="test" />

            <property name="eclipselink.logging.level" value="FINEST"/>
			<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
			<property name="eclipselink.ddl-generation.output-mode" value="database" />
		</properties>

	</persistence-unit>
</persistence>

Back to the top