PDA

View Full Version : Using an Abstract Entity base and providing version and id fields



metronical
Sep 7th, 2009, 05:14 AM
Following on from this thread http://forum.springsource.org/showthread.php?t=77252 I want to provide my own version and id fields for most of my persistent entities so that I can provide XStream annotations on these fields.

I think the best approach here is to provide an AbstractEntity as a base class; but my experiments show that ROO doesn't seem to remove the fields from the generated aspects in the sub-classes. What is the correct way to do this? Do I need to annotate the Abstract class as a RooEntity? Do I have to proved public getters and setters for these fields? I'm also getting warnings like this in the shell 'User provided @javax.persistence.Id field but failed to provide a public 'getId()' method'

Jon

Stefan Schmidt
Sep 7th, 2009, 06:31 PM
Jon,

Can you share the script you are using? Roo should remove the fields from the generated aspects in the sub-classes. If you take a look at the code generated by the sample clinic.roo script you will see that AbstractPerson contains an id and version but not the inheriting Owner...

-Stefan

metronical
Sep 8th, 2009, 02:10 AM
Hi Stefan

Initially I hadn't annotated the AbstractEntity with @RooEntity. It also seems that sometimes I have to 'reboot' the ROO shell in order for it to manage some of the aspect files. I'm not sure what the pattern is, I'll try and come back with something a little more concrete later. All is well for now.

Jon

mikej
Oct 27th, 2009, 12:41 PM
I'm having trouble with this, too. Note that this is for a "master" base entity and is not part of the model in any way.

Here are the (hopefully) reproduceable steps (done with latest build, 359):

Run the following commands in a new Roo project directory:


project --topLevelPackage com.abstracttest --projectName abstracttest

persistence setup --database HYPERSONIC_IN_MEMORY --provider HIBERNATE

class --name ~.domain.AbstractBaseEntity --abstract
field number id --type java.lang.Long
field number version --type java.lang.Integer


Exit Roo and "fix up" AbstractBaseEntity with annotations:


package com.abstracttest.domain;

import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
import javax.persistence.Version;
import javax.persistence.GenerationType;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class AbstractBaseEntity {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;

@Version
@Column(name = "version")
private Integer version;
}


Re-enter Roo and add a new entity that extends AbstractBaseEntity:



roo> entity --name ~.domain.Person --extends ~.domain.AbstractBaseEntity


The response I get is this:


Created SRC_MAIN_JAVA\com\abstracttest\domain\Person.java
Undo create SRC_MAIN_JAVA\com\abstracttest\domain\Person.java
User provided @javax.persistence.Id field but failed to provide a public 'getId()' method in 'com.abstracttest.domain.Person'
roo>


I've also noticed that if I create the Person entity before annotating the AbstractBaseEntity, Roo generates "_id" and "_version" fields on Person_Roo_Entity.aj instead of using the "id" and "version" fields set in AbstractBaseEntity.

mikej
Oct 27th, 2009, 12:48 PM
Added to issue tracker: https://jira.springsource.org/browse/ROO-317