Hello max,
I also want to generate 2 classes as follow:
/*
abstract class AbstractThing // my existing class
public class Thing extends AbstractThing // to-be-generated class
*/
So, I create the mapping file:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="User" table="USER">
<meta attribute="generated-class">AbstractEntity</meta>
<id name="Id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="userName" type="string">
<column name="USER_NAME" />
</property>
<property name="password" type="string">
<column name="PASSWORD" />
</property>
</class>
</hibernate-mapping>
and I expect hibernate tool will generate something like:
Code:
public class User extends AbstractEntity {
private int Id;
private String userName;
private String password;
public User() {}
public User(int Id) {
this.Id = Id;
}
public User(int Id, String userName, String password) {
this.Id = Id;
this.userName = userName;
this.password = password;
}
public int getId() {
return this.Id;
}
public void setId(int Id) {
this.Id = Id;
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString() + Id + "/" + userName;
}
}
And here is my AbstractEntity class:
Code:
public abstract class AbstractEntity implements Serializable {
public static final int NEW_ENTITY_IDENITY = 0;
// Technical identity
private int _id = NEW_ENTITY_IDENITY;
// Real entity type
private Class<? extends Object> _entityType;
// Constructor
protected AbstractEntity(){
// Returns the instance class of entity. E.g. User, Country, etc.
_entityType = this.getClass();
// TODO: need to check it with hibernate again
// when it's a proxy class of target entity. E.g. User00000Proxy -> User
if (_entityType.getName().contains("Proxy"))
_entityType = _entityType.getSuperclass();
}
// Override methods
public String toString() {
return _entityType.getName() + "/" + String.valueOf(_id);
}
public boolean equals(Object obj){
// The same pointer
if (this == obj)
return true; // no need to check more
// it's undefined or not a derived class of AbstractEntity
if (obj == null || !(obj instanceof AbstractEntity))
return false;
// check entity type, and its identity
AbstractEntity target = (AbstractEntity)obj;
if (_entityType != target._entityType || _id != target._id)
return false;
return true;
}
public int hashCode(){
// return the same hash code for entities which has same entity type, and same entity identity
return toString().hashCode();
}
public Object cloneEntity() throws CloneNotSupportedException {
// Clone fields with primitive type
AbstractEntity cloneEntity = (AbstractEntity)super.clone();
// Reset identity
cloneEntity._id = 0; // never clone Identity, it will be a new entity
// TODO: How to know composition associations to clone them?
return cloneEntity;
}
}
but hibernate tool doesn't generate what I expect.
Do you have any idea about how to solve my problem?
Thank you very much