Search code examples
javahibernateormannotationsmapping

How to solve org.hibernate.MappingException which is causing due to inheriting a class


Recently I migrate my project from Hibernate version 3.1 to 4.3 and for better performance I am trying to use annotations instead of my xml mapping.I have the following exception in all the entity class where I am inheriting a class.The error is " org.hibernate.MappingException"

I tried to add targetEntity to the manytoOne class and added @Access(AccessType.PROPERTY) nothing worked. Changing to field annotation is not possible as I have 150+ entity class. I tried to remove the extended class, and its @override methods and the exception was not there, however, I need to add use it.

This is my entity class

    @Entity
    @Table(name = "mapusergroups", catalog = "designdb")
    public class Mapusergroups extends PlanroomMigrationEntity implements 
    java.io.Serializable {

private int idMapUserGroups;
private Groups groups;
private Users users;

public Mapusergroups() {
}

public Mapusergroups(Groups groups, Users users) {
    this.groups = groups;
    this.users = users;
}

@Id
@GeneratedValue(strategy = IDENTITY)

@Column(name = "idMapUserGroups", unique = true, nullable = false)
public int getIdMapUserGroups() {
    return this.idMapUserGroups;
}

public String getIdMapUserGroupsString() {
    return "" + this.idMapUserGroups;
}

public void setIdMapUserGroups(int idMapUserGroups) {
    this.idMapUserGroups = idMapUserGroups;
}

@ManyToOne(fetch = FetchType.LAZY, targetEntity = Groups.class)
@JoinColumn(name = "FK_idGroups", nullable = false)
@PlanroomMigrationEntity.PathUpToCompany
public Groups getGroups() {
    return this.groups;
}

public void setGroups(Groups groups) {
    this.groups = groups;
}

@ManyToOne(fetch = FetchType.LAZY, targetEntity = Users.class)
@JoinColumn(name = "FK_idUsers", nullable = false)
@PlanroomMigrationEntity.ReferenceToUser
public Users getUsers() {
    return this.users;
}

public void setUsers(Users users) {
    this.users = users;
}

@Override
public BaseWrapper<?> getWrapper() {
    return new WMapUserGroups(this);
}

@Override
public PlanroomMigrationDAO getDao() {
    return new MapUserGroupsDAO();
}

@Override
public void attachReferences(JSONObject json, ObjectFinder finder)
        throws JSONException {      this.users = json.has("userId") ? (Users) finder.lookup(Users.class,
json.get("userId").toString()) : null;
this.groups = json.has("groupId") ? (Groups) finder.lookup(
        Groups.class, json.get("groupId").toString()) : null;
}

This is my xml which Is used to convert to annotation

   @XmlType
   public class Mapusergroups extends PlanroomMigrationEntity implements
    java.io.Serializable {

private int idMapUserGroups;
private Users users;
private Groups groups;

public Mapusergroups() {
}

@Deprecated
public Mapusergroups(Users users, Groups groups) {
    this.users = users;
    this.groups = groups;
}

@XmlAttribute
public int getIdMapUserGroups() {
    return this.idMapUserGroups;
}

@XmlAttribute
@XmlID
public String getIdMapUserGroupsString() {
    return "" + this.idMapUserGroups;
}

public void setIdMapUserGroups(int idMapUserGroups) {
    this.idMapUserGroups = idMapUserGroups;
}

@XmlAttribute
@XmlIDREF
@PlanroomMigrationEntity.ReferenceToUser
public Users getUsers() {
    return this.users;
}

public void setUsers(Users users) {
    this.users = users;
}

@XmlAttribute
@XmlIDREF
@PlanroomMigrationEntity.PathUpToCompany
public Groups getGroups() {
    return this.groups;
}

public void setGroups(Groups groups) {
    this.groups = groups;
}

@Override
public BaseWrapper<?> getWrapper() {
    return new WMapUserGroups(this);
}

@Override
public PlanroomMigrationDAO getDao() {
    return new MapUserGroupsDAO();
}

@Override
public void attachReferences(JSONObject json, ObjectFinder finder)
        throws JSONException {
    this.users = json.has("userId") ? (Users) finder.lookup(Users.class,
            json.get("userId").toString()) : null;
    this.groups = json.has("groupId") ? (Groups) finder.lookup(
            Groups.class, json.get("groupId").toString()) : null;
}

I need to use PlanroomMigrationEntity with property annotation. Please help.

This is my stacktrace

org.hibernate.MappingException: Could not determine type for: com.at.project.service.planroom_migration.PlanroomMigrationDAO, at table: mapusergroups, for columns: [org.hibernate.mapping.Column(dao)] at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:349) at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:322) at org.hibernate.mapping.Property.isValid(Property.java:241) at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496) at org.hibernate.mapping.RootClass.validate(RootClass.java:270) at org.hibernate.cfg.Configuration.validate(Configuration.java:1360) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930) at wadetech.DB.base.HibernateUtils.(HibernateUtils.java:111) at wadetech.DB.base.BaseDAO.(BaseDAO.java:43) at wadetech.DB.DAOS.__MaintenanceDAO.(__MaintenanceDAO.java:10) at com.at.project.utils.runtime.RuntimeModifier.HasExecuted(RuntimeModifier.java:127) at wadetech.listeners.ModificationScriptStartupListener.contextInitialized(ModificationScriptStartupListener.java:47) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)


Solution

  • It doesn't have much to do with inheritance. Read the error message. It says the Hibernate doesn't know how to map the property dao of type PlanroomMigrationDAO of your entity.

    This property shouldn't exist in the first place: an entity shouldn't be in charge of creating DAOs.

    But if you really want to stick with that design, you need to tell JPA that dao is not a persistent property. That's the role of the @Transient annotation.