1

I try to use GWT AutoBean with Hibernate. So I wrote the following entity class:

@Entity(name = "Contact")
public interface Contact {

    public interface ContactFactory extends AutoBeanFactory {

        ContactFactory INSTANCE = AutoBeanFactorySource.create(ContactFactory.class);

        AutoBean<Contact> createContact();       
        AutoBean<Contact> wrapContact(Contact contact);
    }

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId();

    @Column(name = "name")
    public String getName();

    public void setId(int id);

    public void setName(String name);
}

But when I call this method:

public void insert(Contact contact) {
    Transaction tx = session.beginTransaction();

    session.save(contact);

    tx.commit();
}

I get the following exception:

org.hibernate.MappingException: Unknown entity: com.sun.proxy.$Proxy11

I googled a bit and I read that AutoBean creates a proxy for accessing the properties of the bean. I think there are maybe a conflict between AutoBean's proxy and Hibernate's proxy. Or maybe Hibernate cannot get the @Entity annotation and get the entity name property.

Someone has used GWT and hibernate autobean ? Thanks you in advance.

Justin C.
  • 301
  • 1
  • 3
  • 9

1 Answers1

0

I don't think you can use @javax.persistence.Entity on an interface. You have to use it with a normal Class.

So you should do this:

Define an interface in the shared package

public interface Contact {

    public int getId();

    public String getId();

    public void setId(int id);

    public void setName(String name);
}

and then create a class that implements that interface in your server package:

public class ContactImpl implements Contact {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    public int getId()  {
        return id;
    }
    ...
}
Ümit
  • 17,379
  • 7
  • 55
  • 74
  • Thanks for your answer. I tried to use an interface with Hibernate and it seems that it works if I move the `@Id`, `@Column` and `@GeneratedValue` annotations next to the accessors methods. But the exception is thrown when I use GWT AutoBean to create an instance of my bean and to persist it. I noticed too that the created GWT AutoBean isn't annotated with the `@Entity` annotation. I thinks it's for this I've this exception. – Justin C. Nov 25 '13 at 23:33
  • I guess the save or update methods of the Hibernate session gets the object type (the class) using the getClass method of the class Object and it can use reflection to get the annotations. Can I force Hibernate to use my interface as type instead of the class of the object to persist ? – Justin C. Nov 25 '13 at 23:33
  • No it's not possible. (see [here](http://stackoverflow.com/questions/2912988/persist-collection-of-interface-using-hibernate) for explaination). Also `AutoBean`s don't need any java.persistence annotations (i.e. `Entity`) they only work on `interfaces`. Apart from that I wouldn't recommend your approach. For example how do you create a `getter` on your `Contact` class that returns a combination of two fields (i..e `getFullname()` -> (firstname + lastname) ? – Ümit Nov 26 '13 at 09:34