views:

30

answers:

2

How would you configure annotations in the following example code? I'd like to stick with JPA annotations only and avoid Hibernate specific dependencies. Is the code below correct?

@Entity
public class RefExample extends RefData {

}

(There will be multiple versions of these classes, RefSomeOtherExample, etc, and one db table per class. Some may add additional fields (columns) but most will simply make use of the basic fields inherited from the "RefData" base class.)

Base class:

@Entity
public abstract class RefData {

    private long id;
    private String code;
    private String desc;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    public long getId() {

        return id;
    }

    public void setId(long id) {

        this.id = id;
    }

    @Column(unique = true, nullable = false, length=8)
    public String getCode() {

        return code;
    }

    public void setCode(String code) {

        this.code = code;
    }

    @Column(unique = true, nullable = false, length=80)
    public String getDesc() {

        return desc;
    }

    public void setDesc(String desc) {

        this.desc = desc;
    }
}

Ultimately I'd like to generate schema creation scripts from this using Hibernate's SchemaExport class. In the case above these two classes should only result in the creation of a single table named "RefExample" with the three columns from "RefData". Will this work?

A: 

You can use one of the inheritance strategy to to this. Your case looks to be the case of Single class for the hierarchy.

Check this

lalit
That article has me stumped. I was actually picturing the Table per concrete class strategy described there (one table per concrete "RefExample" class), but it says that approach is "not popular" AND optional in JPA. None of the three options seem to exactly fit my requirements. Rats!
Crusader
+2  A: 

From JPA 1.0 specification

Both abstract and concrete classes can be entities. Both abstract and concrete classes can be annotated with the Entity annotation, mapped as entities, and queried for as entities.

Entities can extend non-entity classes and non-entity classes can extend entity classes.

As you want a single table, you should use Single Table inheritance

Just define a discriminator column as follows

@Entity
@DiscriminatorColumn(name="REF_TYPE")
public abstract class RefData {

But if you do not want to rely on JPA inheritance strategies, you can use MappedSuperclass instead

@MappedSuperclass
public abstract class RefData {

JPA specification

An entity may inherit from a superclass that provides persistent entity state and mapping information, but which is not itself an entity. Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes.

keep in mind you can not use @Entity and @MappedSupperclass at the same time

Arthur Ronald F D Garcia
@Crusader See here: http://stackoverflow.com/questions/2700680/table-per-subclass-inheritance-relationship-how-to-query-against-the-parent-clas/3017146#3017146 how i workaround perfomance issues when dealing with inheritance. **If you want to use TABLE PER CLASS strategy, the target database must support identity generation strategy**.
Arthur Ronald F D Garcia
Not 100% sure yet but I think MappedSuperclass may be my best option. All these tables are really for is to store values presented in drop down fields which get assigned (one to many) to actual data records.
Crusader
+1 solid answer
Pascal Thivent
MappedSuperclass works perfectly.
Crusader