Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [Texo] Inappropriate @JoinTable JPA mapping
[Texo] Inappropriate @JoinTable JPA mapping [message #873711] Fri, 18 May 2012 22:39 Go to next message
Shahim Essaid is currently offline Shahim EssaidFriend
Messages: 40
Registered: July 2009
Member
The JPA code generated by Texo is adding a @JoinColumn that is causing a problem with the table per class inheritance. This annotation is added to a super entity and it is causing trouble because all the sub entities are having the join columns mapped to the same join table and this is causing trouble for the table per class inheritance. Removing this annotation solves the problem. How can I control the generation of this annotation?

I started a thread about this on the EclipsLink forum (http://www.eclipse.org/forums/index.php/t/352983/) but my post is also copied below.

Best,
Shahim


I am using Texo to generate JPA entities. When I set the inheritance to table per class, the DDL that is generated by EclipseLink is not correct. A simplified version of the entities that show the problem are below. The incorrect generated DDL is:


CREATE TABLE BaseToken_lemmaEntries (EMBUCT_BASETOKEN_DBID INTEGER NOT NULL, lemmaEntries_DBID INTEGER NOT NULL, lemmaEntries_ORDER INTEGER, EMBUCT_SYMBOLTOKEN_DBID INTEGER NOT NULL, lemmaEntries_ORDER INTEGER, EMBUCT_WORDTOKEN_DBID INTEGER NOT NULL, lemmaEntries_ORDER INTEGER,

PRIMARY KEY (EMBUCT_BASETOKEN_DBID, lemmaEntries_DBID, EMBUCT_SYMBOLTOKEN_DBID, EMBUCT_WORDTOKEN_DBID))


Notice the duplicate order columns and the incorrect PK. The problem appears to be that the "token" entities are not saved into their own tables, instead they are all mapped to a single table even with a table per class inheritance.

The original entity hierarchy is much larger and there are more "token" entities. Each of the "token" entities is mapped to the same table above and the PK is larger. The code below should reproduce the problem. I tried this with the current release and the current 2.4 stable build.

Thanks,
Shahim

package db.uima.cas;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import static javax.persistence.InheritanceType.TABLE_PER_CLASS;
import javax.persistence.GeneratedValue;

@Entity(name = "UC_TOP")
@Inheritance(strategy = TABLE_PER_CLASS)
public class TOP {

	@Id
	@GeneratedValue
	int dbid;
}

package db.edu.mayo.bmi.uima.core.type;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderColumn;
import db.uima.cas.TOP;

@Entity(name = "EMBUCT_BASETOKEN")
public class BaseToken extends TOP {

	@Basic(optional = true)
	private int tokenNumber = 0;

	@Basic(optional = true)
	private String normalizedForm = null;

	@Basic(optional = true)
	private String partOfSpeech = null;


	@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST,
			CascadeType.REFRESH }, targetEntity = TOP.class)
	@OrderColumn()
	@JoinTable(name = "BaseToken_lemmaEntries")
	private List<TOP> lemmaEntries = new ArrayList<TOP>();
}


package db.edu.mayo.bmi.uima.core.type;
import javax.persistence.Entity;

@Entity(name = "EMBUCT_SYMBOLTOKEN")
public class SymbolToken extends BaseToken {

}

package db.edu.mayo.bmi.uima.core.type;

import javax.persistence.Basic;
import javax.persistence.Entity;

@Entity(name = "EMBUCT_WORDTOKEN")
public class WordToken extends BaseToken {

	@Basic(optional = true)
	private int capitalization = 0;

	@Basic(optional = true)
	private int numPosition = 0;

	@Basic(optional = true)
	private String suggestion = null;

	@Basic(optional = true)
	private String canonicalForm = null;
}


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
	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">
	<persistence-unit name="uima.db"
		transaction-type="RESOURCE_LOCAL">
		<exclude-unlisted-classes>false</exclude-unlisted-classes>
		<properties>
			<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/uima"/>
			<property name="javax.persistence.jdbc.user" value="root"/>
			<property name="javax.persistence.jdbc.password" value="pass"/>
			<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
			<property name="eclipselink.logging.level" value="INFO"/>
			<property name="eclipselink.jpa.uppercase-column-names" value="true"/>
			<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
			<property name="eclipselink.ddl-generation.output-mode" value="database"/>
		</properties>
	</persistence-unit>
</persistence>

Re: [Texo] Inappropriate @JoinTable JPA mapping [message #873857 is a reply to message #873711] Sat, 19 May 2012 09:05 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5340
Registered: July 2009
Senior Member
Hi Shahim,
I think the JoinTable mapping is fine in itself. To get different join tables for each subclass you have to add an
AssociationOverride annotation to each subentity.

Currently it is not possible to prevent the generation of this annotation. A last resort is to remove the annotation
from the generated java code and then change the javadoc @generated to @generatedNOT
Then Texo won't overwrite this annotation anymore in that location.
But you have to put the generated code in a code repository to not have to continuously make this change.

gr. Martin

On 05/19/2012 12:39 AM, Shahim Essaid wrote:
> The JPA code generated by Texo is adding a @JoinColumn that is causing a problem with the table per class inheritance.
> This annotation is added to a super entity and it is causing trouble because all the sub entities are having the join
> columns mapped to the same join table and this is causing trouble for the table per class inheritance. Removing this
> annotation solves the problem. How can I control the generation of this annotation?
>
> I started a thread about this on the EclipsLink forum (http://www.eclipse.org/forums/index.php/t/352983/) but my post is
> also copied below.
>
> Best,
> Shahim
>
>
> I am using Texo to generate JPA entities. When I set the inheritance to table per class, the DDL that is generated by
> EclipseLink is not correct. A simplified version of the entities that show the problem are below. The incorrect
> generated DDL is:
>
>
> CREATE TABLE BaseToken_lemmaEntries (EMBUCT_BASETOKEN_DBID INTEGER NOT NULL, lemmaEntries_DBID INTEGER NOT NULL,
> lemmaEntries_ORDER INTEGER, EMBUCT_SYMBOLTOKEN_DBID INTEGER NOT NULL, lemmaEntries_ORDER INTEGER, EMBUCT_WORDTOKEN_DBID
> INTEGER NOT NULL, lemmaEntries_ORDER INTEGER,
> PRIMARY KEY (EMBUCT_BASETOKEN_DBID, lemmaEntries_DBID, EMBUCT_SYMBOLTOKEN_DBID, EMBUCT_WORDTOKEN_DBID))
>
>
> Notice the duplicate order columns and the incorrect PK. The problem appears to be that the "token" entities are not
> saved into their own tables, instead they are all mapped to a single table even with a table per class inheritance.
> The original entity hierarchy is much larger and there are more "token" entities. Each of the "token" entities is mapped
> to the same table above and the PK is larger. The code below should reproduce the problem. I tried this with the current
> release and the current 2.4 stable build.
> Thanks,
> Shahim
>
>
> package db.uima.cas;
>
> import javax.persistence.Entity;
> import javax.persistence.Id;
> import javax.persistence.Inheritance;
> import static javax.persistence.InheritanceType.TABLE_PER_CLASS;
> import javax.persistence.GeneratedValue;
>
> @Entity(name = "UC_TOP")
> @Inheritance(strategy = TABLE_PER_CLASS)
> public class TOP {
>
> @Id
> @GeneratedValue
> int dbid;
> }
>
> package db.edu.mayo.bmi.uima.core.type;
>
> import java.util.ArrayList;
> import java.util.List;
> import javax.persistence.Basic;
> import javax.persistence.CascadeType;
> import javax.persistence.Entity;
> import javax.persistence.JoinTable;
> import javax.persistence.ManyToMany;
> import javax.persistence.OrderColumn;
> import db.uima.cas.TOP;
>
> @Entity(name = "EMBUCT_BASETOKEN")
> public class BaseToken extends TOP {
>
> @Basic(optional = true)
> private int tokenNumber = 0;
>
> @Basic(optional = true)
> private String normalizedForm = null;
>
> @Basic(optional = true)
> private String partOfSpeech = null;
>
>
> @ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST,
> CascadeType.REFRESH }, targetEntity = TOP.class)
> @OrderColumn()
> @JoinTable(name = "BaseToken_lemmaEntries")
> private List<TOP> lemmaEntries = new ArrayList<TOP>();
> }
>
>
> package db.edu.mayo.bmi.uima.core.type;
> import javax.persistence.Entity;
>
> @Entity(name = "EMBUCT_SYMBOLTOKEN")
> public class SymbolToken extends BaseToken {
>
> }
>
> package db.edu.mayo.bmi.uima.core.type;
>
> import javax.persistence.Basic;
> import javax.persistence.Entity;
>
> @Entity(name = "EMBUCT_WORDTOKEN")
> public class WordToken extends BaseToken {
>
> @Basic(optional = true)
> private int capitalization = 0;
>
> @Basic(optional = true)
> private int numPosition = 0;
>
> @Basic(optional = true)
> private String suggestion = null;
>
> @Basic(optional = true)
> private String canonicalForm = null;
> }
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <persistence version="2.0"
> 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">
> <persistence-unit name="uima.db"
> transaction-type="RESOURCE_LOCAL">
> <exclude-unlisted-classes>false</exclude-unlisted-classes>
> <properties>
> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/uima"/>
> <property name="javax.persistence.jdbc.user" value="root"/>
> <property name="javax.persistence.jdbc.password" value="pass"/>
> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
> <property name="eclipselink.logging.level" value="INFO"/>
> <property name="eclipselink.jpa.uppercase-column-names" value="true"/>
> <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
> <property name="eclipselink.ddl-generation.output-mode" value="database"/>
> </properties>
> </persistence-unit>
> </persistence>
>
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Re: [Texo] Inappropriate @JoinTable JPA mapping [message #876113 is a reply to message #873857] Wed, 23 May 2012 23:37 Go to previous messageGo to next message
Shahim Essaid is currently offline Shahim EssaidFriend
Messages: 40
Registered: July 2009
Member
Quote:
I think the JoinTable mapping is fine in itself. To get different join tables for each subclass you have to add an
AssociationOverride annotation to each subentity.


Yes, that would take care of it but I am trying to automate the generation of the JPA code for end users and they can't deal with these details. For now I am stripping all the @JoinTable annotations from the generated code with a simple script.

I am not sure if it necessary for Texo to explicitly add the defaults to the generated code. It is causing more trouble than benefit in my case. EclispeLink would have no problems if there wasn't an explicit @JoinTable on the super entity.

A possible solution could be to not add any default annotations to the generated code unless the annotations are in the generation model. To get the default annotations, a user could use a "generate default mappings generation model" command to create or augment the generation model with the defaults that would then be used during code generation.

Best,
Shahim

[Updated on: Wed, 23 May 2012 23:46]

Report message to a moderator

Re: [Texo] Inappropriate @JoinTable JPA mapping [message #876176 is a reply to message #876113] Thu, 24 May 2012 03:56 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5340
Registered: July 2009
Senior Member
Hi Shahim,
Yes I feel currently also that in some cases that sometimes too many annotations are generated (like joincolumns without
a name for example). In this case there is an option on the epackage orm annotation:
useJoinTablesForNonContainment
as a default it is set to true, can you see what happens with this option if you set it to false?

probably you get a joincolumn annotation, which doesn't need to be there so that the ORM takes care of defaulting the
association.

gr. Martin

On 05/24/2012 01:37 AM, Shahim Essaid wrote:
> Quote:
>> I think the JoinTable mapping is fine in itself. To get different join tables for each subclass you have to add an
>> AssociationOverride annotation to each subentity.
>
>
> Yes, that would take care of it but I am trying to automate the generation of the JPA code for end users and they can't
> deal with these details. For now I am stripping all the @JoinTable annotations from the generated code with a simple
> script.
>
> I am not sure if it necessary for Texo to explicitly add the defaults to the generated code. It is causing more trouble
> than benefit in my case. EclispeLink would have no problems if there wasn't an explicit @JoinTable on the super entity.
>
> A possible solution could be to not add any default annotations to the generated code unless the annotations are in the
> generation model. To get the default annotations, a user could use a "generate default mappings generation model"
> command to create or augment the generation model with the defaults that would then be used during code generation.
>
> Best,
> Shahim


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@xxxxxxxx - mtaal@xxxxxxxx
Web: www.springsite.com - www.elver.org
Previous Topic:[EEF] component binding and eOperations
Next Topic:Problems when running the EMF Store client
Goto Forum:
  


Current Time: Fri Dec 19 15:15:11 GMT 2014

Powered by FUDForum. Page generated in 0.02350 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software