hibernate - Removing both @Entity and @MappedSuperclass in the same class -


one of existing classes (class a) has both @entity , @mappedsuperclass annotations , child class (class b extends a) has @entity.

although in not right, working fine hibernate-entitymanager 3.3. both entities have own table , table class b has same columns class plus 1 additional column corresponds own property.

i'm trying fix mistake sitting there years can migrate 3.6 not allow both annotations. (it throws org.hibernate.annotationexception: entity cannot annotated both @entity , @mappedsuperclass:)

i tried replace @mappedsuperclass @inheritance(strategy=inheritancetype.table_per_class) i'm getting runtime errors when loading class 1 of properties set of b.

org.hibernate.util.jdbcexceptionreporter.logexceptions(jdbcexceptionreporter.java:78) - unknown column 'a.createddatetime' in 'order clause' org.hibernate.exception.sqlgrammarexception: not initialize collection: [com.acmy.anotherclassc.classbset#975905b5-d59c-4e53-98dd-30cf39b0c831] 

what smooth way of fixing class uses both @entity , @mappedsuperclass use @entity , @inheritance? need sql fix existing data?

you can remove @mappedsuperclass annotation - leave @entity , @inheritance(strategy=inheritancetype.table_per_class).

you can not use @generatedvalue(strategy = generationtype.auto) i.e. @generatedvalue(strategy = generationtype.table)

update

an example - works me

register generator in package-info class

package-info.java

@genericgenerator(name = "system-uuid", strategy = "uuid") package db;  import org.hibernate.annotations.genericgenerator; 

your class classa.java

package db;  import java.io.serializable; import java.util.objects; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.inheritance; import javax.persistence.inheritancetype;  @entity @inheritance(strategy = inheritancetype.table_per_class) public class classa implements serializable {     private static final long serialversionuid = 1l;      @id     @generatedvalue(generator = "system-uuid")     private string id;      private string value;      public string getid() {         return id;     }      public void setid(string id) {         this.id = id;     }      public string getvalue() {         return value;     }      public void setvalue(string value) {         this.value = value;     }      @override     public int hashcode() {         int hash = 7;         hash = 67 * hash + objects.hashcode(this.id);         return hash;     }      @override     public boolean equals(object obj) {         if (obj == null) {             return false;         }         if (getclass() != obj.getclass()) {             return false;         }         final classa other = (classa) obj;         if (!objects.equals(this.id, other.id)) {             return false;         }         return true;     }      @override     public string tostring() {         return "db.classa[ id=" + id.tostring() + " ]";     } } 

and inherited class b classb.java

package db;  import javax.persistence.discriminatorvalue; import javax.persistence.entity;  @entity @discriminatorvalue("b") public class classb extends classa {     private static final long serialversionuid = 1l;      @override     public string tostring() {         return "db.classb[ id=" + getid() + " ]";     } } 

last not least main class testing

testdb.java

package db;  import java.util.list; import javax.persistence.entitymanager; import javax.persistence.entitymanagerfactory; import javax.persistence.persistence;  public class testdb {     entitymanager em;      public static void main(string[] args) {         new testdb().start();     }      public void start() {         entitymanagerfactory emf = persistence.createentitymanagerfactory("dbtest");         em = emf.createentitymanager();         em.gettransaction().begin();         classa = new classa();         a.setvalue("a");         em.persist(a);         = new classa();         a.setvalue("b");         em.persist(a);         classb b = new classb();         b.setvalue("c");         em.persist(b);         em.gettransaction().commit();          em.gettransaction().begin();         list list = em.createquery("from " + classa.class.getsimplename() + " c order c.value").getresultlist();         system.out.println("list " + list);         em.gettransaction().commit();     } } 

the persistence.xml looks this. use h2database im-memory database

<?xml version="1.0" encoding="utf-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">     <persistence-unit name="dbtest" transaction-type="resource_local">         <provider>org.hibernate.ejb.hibernatepersistence</provider>         <class>db.classa</class>         <class>db.classb</class>         <properties>             <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:"/>             <property name="javax.persistence.jdbc.user" value="app"/>             <!--property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.clientdriver"/-->             <property name="javax.persistence.jdbc.password" value="app"/>             <property name="hibernate.cache.provider_class" value="org.hibernate.cache.nocacheprovider"/>             <property name="javax.persistence.schema-generation.database.action" value="create"/>             <property name="hibernate.hbm2ddl.auto" value="update"/>         </properties>     </persistence-unit> </persistence> 

Comments

Popular posts from this blog

Why can rails not find a route created by a helper? -

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -