0

I have a number of POJOs auto-generated by Hibernate's reverse engineering feature. They look like:

package db;
/**
 * Pojo generated by hbm2java
 */
public class Pojo implements java.io.Serializable 
{
    private int id;

    public Pojo() {
    }

    public Pojo(int id) {
        this.id = id;
    }

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

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

I need to do various things with some of these POJOs and thus created derived classes, such as:

package input;

public class Pojo extends db.Pojo
{
    public Pojo() 
    {
        // TODO Auto-generated constructor stub
        super();
    }

    public void doSomething()
    {}
}

At some point I need to store new instances of these POJOs using a Session object:

input.Pojo pj = new input.Pojo();
pj.doSomething();
session.save((db.Pojo) pj);

Even though the POJO instance is being cast to the generated parent class, Hibernate gets upset and spits out the familiar exception:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: input.Pojo

I understand I may be pushing Hibernate a bit, but I would prefer to leave the generated POJOs unchanged (particularly since the database is expected to evolve). Is it possible to avoid this exception? Say, by "forcing" Hibernate to recognise the cast? Or should this be achieved some other way?

1
  • Why can't you move doSomething() into the entity class? Commented May 28, 2016 at 22:49

2 Answers 2

1

I have discussed this issue at the Hibernate Forum and IRC channel, and so far no proper mechanism to quell this exception came up. Hibernate does not seem to have been designed to deal with this somewhat apparently uncommon situation.

In this particular case, I can work around the Inheritance pattern through Composition. This may not suit all similar situations, but fully reuses the code without exacerbating other maintenance issues.

The input class then looks like:

package input;

public class Pojo
{
    public Pojo() 
    {
        pojo = new db.Pojo();
    }

    protected db.Pojo pojo;

    public db.Pojo getPersistentObject() {return pojo;}

    public void doSomething()
    {}
}

And usage goes like:

input.Pojo pj = new input.Pojo();
pj.doSomething();
session.save(pj.getPersistentObject());
Sign up to request clarification or add additional context in comments.

Comments

0

There are some options that you can try.

  1. Transform the input.POJO to db.POJO.

    a. Manual Transform call getters and setters.

    b. Use Dozer or similar tool.

  2. [Not usefull in this scenario]You can have the doSomething() method directly in Entity

I would do 1 (b)

Details on 1. : You create an object of input.POJO object1 (non - Entity) and map all the values to an object of db.POJO object2 (Entity).

Example:

object2.setAtt1(object1.getAtt1());
object2.setAtt2(object1.getAtt2());

Now you pass the object2 to session.save(object2); And it should work.

You can use dozer which will do the staff of copying values from one object to another automatically

6 Comments

Could you explain: a) what "Transform" exactly means; and b) how can the reverse engineering configuration be set to generate the doSomething() method?
Agreed Point 2 is useless in this case.
I am not sure I fully understand your answer, but it seems you are suggesting me to replicate all properties and respective getters/setters in the derived class. That would pretty much dismiss the whole purpose of inheritance.
As your input.POJO extends db.POJO it is not required to re create getters and setters. But you will have to create different instance of db.POJO and copy the values to it.
Now I see what you mean. However, this implies manual changes to this hypothetical copying methods every time the data base model is updated. This sort of dependencies exacerbate the issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.