show
hide

Java Hibernate Tutorial Part 3

In the first part of the tutorial I have explained in detail about how to map multiple classes to multiple tables.


Then in the second part of the tutorial I have explained in detail about the use of discriminator column and the discriminator value by giving self explanatory example.


The tutorial also cover’s the concept of the use of the subclass and how to map the subclasses in the hibernate mapping file.


The example also cover the Table per class scenario that is we have One Table for class and its sub classes.


Program code


Games.cfg.xml





oracle.jdbc.driver.OracleDriver<  /property>
jdbc:oracle:thin:@localhost:1521:XE
org.hibernate.dialect.Oracle10gDialect
sameer
sameer

false

true
true
true







    
Games.java
import java.io.Serializable;
public class Games implements Serializable {
private int id;
    private String name;
    private String type;
    private long space;
    public Games(){}
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public long getSpace() {
        return space;
    }
    public void setSpace(long space) {
        this.space = space;
    }
}
Arcade.java
public class Arcade extends Games{
    public Arcade(){}
}
Rpg.java
public class Rpg extends Games{
    public void Rpg(){}

}
Final.java
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class Final {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
            Configuration cfg = new Configuration();
            cfg.configure(“games.cfg.xml”);
            SessionFactory sf = cfg.buildSessionFactory();
            Session s = sf.openSession();
            Transaction tx = s.beginTransaction();
            Games g = new Games();
            g.setId(1);
            g.setName(“nfs”);
            g.setSpace(500);
            Rpg r = new Rpg();
            r.setId(2);
            r.setName(“ninjagaiden”);
            r.setSpace(7000);
            Arcade a = new Arcade();
            a.setId(3);
            a.setName(“tekken”);
            a.setSpace(600);
            s.save(g);
            s.save(r);
            s.save(a);
            s.flush();
            tx.commit();
    }
}
Press Esc to close