Is it possible to build a JPA object by extending a POJO?

Suppose I have the following POJO:

public class MyThing { private int myNumber; private String myData; //assume getter/setter methods } 

Is it possible to extend this POJO as a JPA object?

 @Entity @Table(name = "my_thing") public class MyThingEntity extends MyThing implements Serializable { @Column(name = "my_number") //????????? @Column(name = "my_data") //???????? } 

I want to keep the POJO separate from the JPA object. POJO lives in another project and is often used without a save layer, my project wants to save it in a database and do this without the overhead of comparing POJO with an entity and vice versa.

I understand that JPA entities are POJOs, but to use it I would have to include a library that implements javax.persistence, and other projects that use the same base object are not needed for the save layer.

Is it possible? Is that a good idea?

+49
java jpa
Mar 25
source share
2 answers

JPA Specification Specification

Entities can extend non-entity classes as well as entity classes , and classes without entities can extend entity classes.

@ javax.persistence.MappedSuperclass annotations allows you to define this kind of display

 @MappedSuperclass public class MyThing implements Serializable { private int myNumber; private String myData; // getter and setter's } 

and

 @Entity @Table(name="MY_THING") public class MyThingEntity extends MyThing { } 

As stated in the JPA specification

The designation MappedSuperclass denotes a class whose mapping information applies to objects that inherit from it .

and

The class indicated by the MappedSuperclass annotation can be mapped in the same way as an object, except that mappings will only apply to its subclasses , since there is no table for the mapped superclass itself.

If you need to override a property defined by MyThing, use @AttributeOverride (if you want to override one property) or @AttributeOverrides (if you want to override several properties)

 @Entity @Table(name="MY_THING") @AttributeOverride(name="myData", column=@Column(name="MY_DATA")) public class MyThingEntity extends MyThing { } 

and

 @Entity @Table(name="MY_OTHER_THING") @AttributeOverrides({ @AttributeOverride(name="myData1", column=@Column(name="MY_DATA_1")), @AttributeOverride(name="myData2", column=@Column(name="MY_DATA_2")) }) public class MyOtherThingEntity extends MyThing { } 

If you do not want to change the base class, you can use xml to define it as @MappedSuperClass

Be aware: by default, the save provider will search in the META-INF directory for a file named orm.xml

 <?xml version="1.0" encoding="UTF-8"?> <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0"> <mapped-superclass class="MyThing"> </mapped-superclass> </entity-mappings> 

Nothing more . If you want to override a property, use @AttributeOverride, as shown above.

+63
Mar 25 '10 at 15:39
source share

Maybe:

  • you can map it to XML - make orm.xml (according to the orm schema ) and map the columns of your POJO without even expanding it. It will be included in JPA in one environment, and POJO in another.
  • override only getter methods and annotate them - (I'm not sure if this will work)

However, I do not think this is necessary. Just comment on your POJO and add a compilation dependency to your projects. Then each project will decide whether it will use them as JPA entities or not.

+6
Mar 25
source share



All Articles