Problem with JPA Model Inheritance with strategy = InheritanceType.TABLE_PER_CLASS

2,285 views
Skip to first unread message

green

unread,
May 30, 2010, 1:50:50 AM5/30/10
to play-fr...@googlegroups.com
Hi 

Someone has ever tried "TABLE_PER_CLASS" inheritance with Play Model? The immediate problem I've encountered is : "Cannot use identity column key generation with mapping for: models.SubModelX". I tried to fix this problem by adding new ID to sub model class following this thread: http://stackoverflow.com/questions/916169/cannot-use-identity-column-key-generation-with-union-subclass-table-per-class. The old problem has gone, however I was stucked with new exception: "Unable to define/override @Id(s) on a subclass".

Thx,
Green

Archer Yongjian Liu

unread,
May 30, 2010, 3:11:27 AM5/30/10
to play-fr...@googlegroups.com
I have encounter this problem before. 
What do you mean 'by adding new ID to sub model class' ?
Can you give an example ?
Do you use Java or Scala ?

When I do this in scala: 
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract class SupperClass extends Model{
   @Id
   @GeneratedValue(strategy =GenerationType.TABLE, generator = "SEQ_TABLE")
   @TableGenerator(name = "SEQ_TABLE")
   override def getId() = super.getId()          /* ---------------- it does not take effect here    ----------------*/

   // Other SupperClass attributes
}

@Entity
class  MySubClass1 extends SupperClass{
  
   //Other MySubClass1 attributes
}

----------------------------------------------------------------------------------------------------------------------------------------------------------

However, we can't override id attribute in scala  because it is defined as mutable variable in superclass

So, I create a new class named MyModel , which only replace the id definition as follow:

class MyModel extends JPABase{
     ....

    // only changes here 
    @Id
   @GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQ_TABLE")
   @TableGenerator(name = "SEQ_TABLE")
    var id: Long = _
}

Then redefine the poso:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
abstract class SupperClass extends MyModel{
   // Other SupperClass attributes
}

@Entity
class  MySubClass1 extends SupperClass{
  
   //Other MySubClass1 attributes
}


-----------------------------------------------------------------------------------------------------------
Any good solution or suggestion ?



--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.



--
Thank you.

green

unread,
May 30, 2010, 8:03:32 AM5/30/10
to play-fr...@googlegroups.com
Thanks Archer. By "adding an new ID" I mean override ID field definition in play.db.jpa.Model in the sub class.

BTW, what is JPABase in your code?

Archer Yongjian Liu

unread,
May 30, 2010, 8:22:06 AM5/30/10
to play-fr...@googlegroups.com
Do you want to override the id  field mapping  using @AttributeOverride ?

JPABase is the class  provided by Play!

green

unread,
May 30, 2010, 8:37:45 AM5/30/10
to play-fr...@googlegroups.com
It's the case in my Play version (v1.0.2.1). The model hierarchy in this version is

models.MyBaseModel - > play.db.jpa.Model ->play.db.jpa.JPASupport

It looks "SupperClass" in your code is missing from my model hierarchy. I will try it and come back

Thanks!
Green

Guillaume Bort

unread,
May 30, 2010, 10:42:21 AM5/30/10
to play-fr...@googlegroups.com
You can extend the JPASupport class directly if you don't want the
default id field.

On Sunday, May 30, 2010, green <green...@gmail.com> wrote:
> It's the case in my Play version (v1.0.2.1). The model hierarchy in this version is
> models.MyBaseModel - > play.db.jpa.Model ->play.db.jpa.JPASupport
> It looks "SupperClass" in your code is missing from my model hierarchy. I will try it and come back
>
> Thanks!Green
>
> On Sun, May 30, 2010 at 10:22 PM, Archer Yongjian Liu <yl...@kalengo.com> wrote:
> Do you want to override the id  field mapping  using @AttributeOverride ?
> JPABase is the class  provided by Play!
>
>
>
>
> On Sun, May 30, 2010 at 8:03 PM, green <green...@gmail.com> wrote:
> Thanks Archer. By "adding an new ID" I mean override ID field definition in play.db.jpa.Model in the sub class.
>
>
> BTW, what is JPABase in your code?
>
> On Sun, May 30, 2010 at 5:11 PM, Archer Yongjian Liu <yl...@kalengo.com> wrote:
> I have encounter this problem before. What do you mean 'by adding new ID to sub model class' ?
>
>
>

> Can you give an example ?Do you use Java or Scala ?
> When I do this in scala: @Entity@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)abstract class SupperClass extends Model{


>
>
>
>    @Id   @GeneratedValue(strategy =GenerationType.TABLE, generator = "SEQ_TABLE")   @TableGenerator(name = "SEQ_TABLE")   override def getId() = super.getId()          /* ---------------- it does not take effect here    ----------------*/
>
>
>
>
>    // Other SupperClass attributes
> }

> @Entityclass  MySubClass1 extends SupperClass{     //Other MySubClass1 attributes


> }
>
>
>
>
>
> ----------------------------------------------------------------------------------------------------------------------------------------------------------
> However, we can't override id attribute in scala  because it is defined as mutable variable in superclass
>
>
>
>
> So, I create a new class named MyModel , which only replace the id definition as follow:
> class MyModel extends JPABase{     ....
>     // only changes here
>
>
>
>     @Id   @GeneratedValue(strategy = GenerationType.TABLE, generator = "SEQ_TABLE")   @TableGenerator(name = "SEQ_TABLE")    var id: Long = _}
>

> Then redefine the poso:@Entity@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)abstract class SupperClass extends MyModel{   // Other SupperClass attributes
>
>
>
> }
> @Entityclass  MySubClass1 extends SupperClass{     //Other MySubClass1 attributes

dao

unread,
Jul 7, 2011, 3:54:22 AM7/7/11
to play-fr...@googlegroups.com
what do you mean by extending the JPASupport? I am in the same case as Green. 
When I run my app I've got this when initializing JPA:
JPA error
A JPA error occurred (Unable to build EntityManagerFactory): Cannot use identity column key generation with <union-subclass> mapping for: models.OooiMessage


According to this thread, 
if you use TABLE_PER_CLASS, you have to use this ID generation strategy: @GeneratedValue(strategy = GenerationType.TABLE)

Unfortunatelly, the Play Model class does not use this strategy. How can I handle this?

thanx
Reply all
Reply to author
Forward
0 new messages