Hi Keisuke,
I attached 3 Java Files and my hibernate configuration in this reply. The 2 classes Cat and DomesticCat should give you a starting point with Hibernate Annotations and the AnnoSchemaExport class shows you how to generate SQL for schema generation and export it to a database. As you will see, no mapping files are needed anymore which I think facilitates using Hibernate enormously. I strongly advise you to have a look at the Hibernate Annotations documentation which I think is very good.
Uli
Code:
@Entity
@Table(name="cat")
@Inheritance(strategy=InheritanceType.JOINED)
public class Cat {
/* specifying the @Id annotation here corresponds to
* access="field"
*/
protected int id;
/* specifying the @Basic annotation here corresponds to
* access="field"
*/
protected String sex;
public Cat() {}
public Cat(String sex) { this.sex = sex; }
@Id
@GeneratedValue(generator="autoinc")
@GenericGenerator(name="autoinc", strategy="identity")
public int getId() { return id; }
public void setId(int id) { this.id = id; }
/* omitting an annotation here is the same as providing
* @Basic
* The @Basic annotation allows to specify the FetchType,
* e.q. @Basic(fetch=FetchType.LAZY)
*/
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
public String toString() {
return "I'm a " + sex + " Cat with ID " + id;
}
}
Code:
@Entity
@Table(name="domesticcat")
public class DomesticCat extends Cat {
String name;
public DomesticCat() { super(); }
public DomesticCat(String sex, String name) {
super(sex);
this.name = name;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String toString() {
return name + " is a " + sex + " DomesticCat with ID " + id;
}
}
Code:
public class AnnoSchemaExport {
public static void main(String[] args) {
Configuration cfg = new AnnotationConfiguration().configure();
SchemaExport se = new SchemaExport(cfg);
se.create(true,true);
}
}
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost/xdb</property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<mapping class="test.hb.Cat" />
<mapping class="test.hb.DomesticCat" />
</session-factory>
</hibernate-configuration>
P.S.: I left out the imports for brevity.