Java Tutorial/Hibernate/Mapping Inheritance — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:02, 1 июня 2010
Class In Hierarchy Has Its Own Table
File: Main.java
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
public class Main {
public static void main(String[] args) throws Exception {
HibernateUtil hibernateUtil = new HibernateUtil();
hibernateUtil
.executeSQLCommand("create table CD(id varchar,title varchar,artist varchar,purchaseDate date,cost decimal(6,2))");
hibernateUtil
.executeSQLCommand("create table SECD(id varchar,title varchar,artist varchar,purchaseDate date,cost decimal(6,2), newfeatures varchar)");
hibernateUtil
.executeSQLCommand("create table ICD(id varchar,title varchar,artist varchar,purchaseDate date,cost decimal(6,2),languages varchar, region int)");
Session session = hibernateUtil.getSession();
CD cd = new CD();
cd.setArtist("artist");
cd.setTitle("title");
String id = (String)session.save(cd);
System.out.println(id);
SpecialEditionCD scd = new SpecialEditionCD();
scd.setNewfeatures("new");
session.save(scd);
InternationalCD icd = new InternationalCD();
icd.setLanguages("ENG");
session.save(icd);
session.flush();
List cds = session.createQuery("from CD").list();
Iterator iter = cds.iterator();
while (iter.hasNext()) {
CD cd1 = (CD) iter.next();
System.out.println(cd1.getArtist());
}
session.close();
hibernateUtil.checkData("select * from CD");
hibernateUtil.checkData("select * from SECD");
hibernateUtil.checkData("select * from ICD");
}
}
Class In Hierarchy Maps To One Table
File: Main.java
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
public class Main {
public static void main(String[] args) throws Exception {
HibernateUtil hibernateUtil = new HibernateUtil();
hibernateUtil
.executeSQLCommand("create table CD(id varchar,title varchar,artist varchar,purchaseDate date,cost decimal(6,2),languages varchar, newfeatures varchar)");
Session session = hibernateUtil.getSession();
CD cd = new CD();
cd.setArtist("artist");
cd.setTitle("title");
String id = (String)session.save(cd);
System.out.println(id);
SpecialEditionCD scd = new SpecialEditionCD();
scd.setNewfeatures("new");
session.save(scd);
InternationalCD icd = new InternationalCD();
icd.setLanguages("ENG");
session.save(icd);
session.flush();
session.close();
hibernateUtil.checkData("select * from CD");
}
}
Mapping Class Hierarchy
File: Main.java
import org.hibernate.Session;
public class Main {
public static void main(String[] args) throws Exception {
HibernateUtil hibernateUtil = new HibernateUtil();
hibernateUtil
.executeSQLCommand("create table Product(id int, name varchar, description varchar, price decimal(6,2), supplierid int)");
hibernateUtil
.executeSQLCommand("create table Supplier (id int , name varchar)");
hibernateUtil
.executeSQLCommand("create table Software(id int, name varchar, description varchar, price decimal(6,2), supplierid int, version varchar)");
Session session = hibernateUtil.getSession();
Supplier superCorp = new Supplier();
superCorp.setName("Supplier1");
session.save(superCorp);
Supplier megaInc = new Supplier();
megaInc.setName("Supplier2");
session.save(megaInc);
Product mouse = new Product("Product1","first product", 20.0);
mouse.setSupplier(superCorp);
superCorp.getProducts().add(mouse);
session.flush();
Product mouse2 = new Product("Product2","second product", 22.0);
mouse2.setSupplier(superCorp);
superCorp.getProducts().add(mouse2);
Product keyboard = new Product("Product3", "third product", 30.0);
keyboard.setSupplier(megaInc);
megaInc.getProducts().add(keyboard);
Software webBrowser = new Software("Web Browser","new browser", 75.0, "2.0");
webBrowser.setSupplier(superCorp);
superCorp.getProducts().add(webBrowser);
Software email = new Software("Email","email client", 49.99, "4.1 Edition");
email.setSupplier(megaInc);
megaInc.getProducts().add(email);
session.flush();
session.close();
hibernateUtil.checkData("select * from Product");
hibernateUtil.checkData("select * from Software");
hibernateUtil.checkData("select * from Supplier");
}
}