2

I'm extending from a utility base class that maps id to columns

@MappedSuperclass
public abstract class BaseDTO
{
     @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int id;
    public int getId() {
        return id;}

    public void setId(int id) {
        this.id = id;}
}

This class is used in many derived classes to provide simplified access and operations to tables that contain id columns. i.e.

BaseDTO->DBAbstractClass1->DBAbstract2->MyClassDTO
BaseDTO->DBAbstractClass1->DBAbstract3->AnotherADTO
BaseDTO->DBAbstractClass2->DBAbstract4->AnotherBDTO

etc.

Now I find I have a table mapped to MyClassDTO that does not have an 'id' column (it has a column called 'identifier') but due to architecural constraints, exending as per normal causes:

[org.hibernate.util.JDBCExceptionReporter] SQL Error: 1054, SQLState: 42S22
[org.hibernate.util.JDBCExceptionReporter] Unknown column 'this_.id' in 'field list'

I have tried overriding the id member as @Transient, adding a new @Id etc. but always get the error above.

Either I find a way of 'ignoring' or replacing the inherited id or I need to copy my inherited classes and not extend BaseDTO, which is something that a good programmer should avoid.

I have seen something simlar at Hibernate : How override an attribute from mapped super class but I's not sure it solves my exact problem.

It's probably a simple override, but want to be careful that there are no side affects. Any takers?

kaya3
  • 47,440
  • 4
  • 68
  • 97
Anthony Palmer
  • 944
  • 10
  • 15

2 Answers2

1

I have found no simple elegant solution to this.

What I have done is reverse the hierarchy to create two HelperDTO classes, one that extends the other and the second includes the id property. HelperDTO does not have Id member and HelperWithIdDTO has an Id member i.e.

HelperDTO->MyClassDTO (classes that do not use id)

and

HelperDTO->HelperWithIdDTO->OtherDTO (classes that use id)
Anthony Palmer
  • 944
  • 10
  • 15
0

Try override in your subclass with these changes to the super ?

@MappedSuperclass
public abstract class BaseDTO
{

    public int id;
    public int getId() {
        return id;}

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public void setId(int id) {
        this.id = id;}
}
MikePatel
  • 2,593
  • 2
  • 24
  • 38
  • I Did as you asked above, and added the following to the dervied class ` @Transient` `public int id;` `@Id` `@GeneratedValue(strategy=GenerationType.AUTO)` `private long other_id;` and got the following error Initial SessionFactory creation failed.org.hibernate.AnnotationException: No identifier specified for entity: com.mine.something.AnotherDTO – Anthony Palmer May 08 '12 at 11:30