Hi guys,
I'm experiencing the same kind of problem as you had in the past.
I know this post is pretty old, but anyway the problem still sometime happens.
So, for me I have more or less the same issue but with different classes and the thing is I can't use @MappedSuperClass on the base classes because they are coming from an external library.
Anyway after fighting a bit I successfully make it (half) worked by using the orm.xml file and declaring the base classes with the <mapped-superclass /> element.
Now I'm still having an issue.
The problem is that one of the class I have has a enum property and I want to use the EnumUserType provided by Gavin King on the hibernate wiki (
http://www.hibernate.org/272.html) to prevent me mapping the enum in the database (we all know how mysql sucks with enum types).
So I wanted to use the specific hibernate annotation @Type on the override method but then this is not working at all, it's like hibernate does not see the annotation since the base class is not annotated.
Here is the example to reproduce the problem:
<<Requirement enum>>
Code:
package com.ppp.common.types;
public enum Requirement {
OPTIONAL,
REQUIRED,
MANDATORY;
}
<<BaseClass class>>This is the base class I can't modify.
Code:
package com.ppp.common.types;
class BaseClass {
private String property;
private Requirement requirement;
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public Requirement getRequirement() {
return requirement;
}
public void setRequirement(Requirement requirement) {
this.position = requirement;
}
}
<<orm.xml>>Here is the orm.xml mapping I'm using to declare the mapped-superclass.
Code:
<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="com.ppp.common.types.BaseClass">
<attributes>
<basic name="property" />
<basic name="requirement">
<column name="requirement" column-definition="tinyint" nullable="false" />
</basic>
</attributes>
</mapped-superclass>
</entity-mappings>
<<BaseEntity class>>Here is the implementation I'm using to map the base class into the DB.
Code:
@Entity
class BaseEntity extends BaseClass implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
@Type(
type = "com.myorg.common.hibernate.EnumUserType", // com.myorg.common.hibernate.EnumUserType is based on the EnumUserType provided by Gavin King.
parameters = {
@Parameter(name = "enumClassName", value = "com.ppp.common.types.Requirement")
}
)
public Requirement getRequirement() {
return super.getRequirement();
}
}
So the more I think about this, the more I think it's a kind of a bug... is it? I'm not really sure because the hibernate documentation clearly says:
Quote:
2.2.4.4. Inherit properties from superclasses
This is sometimes useful to share common properties through a technical or a business superclass without including it as a regular mapped entity (ie no specific table for this entity). For that purpose you can map them as @MappedSuperclass.
[ ... ]
Note
Properties from superclasses not mapped as @MappedSuperclass are ignored.
Note
Any class in the hierarchy non annotated with @MappedSuperclass nor @Entity will be ignored.
But still, the class itself is not annotated, but the orm.xml mapping file declares the <mapped-superclass /> which should be the equivalent...
Any comments or suggestions getting the @Type working are greatly appreciated.
Cheers,
/Benoit