Prezimovanje (2. del): konfiguracija in uporaba
Konfiguracija mirovanja v aplikaciji Java
- Namestite JDK 1.4 ali JDK 1.5.
- Prenesite različico 3 s hibernate.org
- Upoštevajte kozarce mirovanja v poti poti.
- Določite konfiguracijsko datoteko za mirovanje (hibernate.cfg.xml):
Ta datoteka mora biti shranjena v imeniku, ki vsebuje izvorno kodo aplikacije JAVA (priporočeno).
Spodaj je primer datoteke hibernate.cfg.xml s potrebnimi komentarji:
PUBLIC "- // DTD // Hibernate / Hibernate konfiguracije."
"[//hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
jdbc: oracle: thin: @dbServer: 1521: db
oracle.jdbc.OracleDriver
org.hibernate.dialect.Oracle9Dialect
dbusername
dbpassword
prav
prav
prav
1
false
50
prav
org.hibernate.connection.C3P0ConnectionProvider
3
180
100
10
1000
org.hibernate.cache.NoCacheProvider
Določite datoteke preslikav hbm:
To je nabor datotek, ki omogočajo preslikavo med bazo podatkov entitete in povezanimi objekti JAVA.
Celotne poti vseh datotek za preslikavo hbm so opisane v konfiguracijski datoteki (hibernate.cfg.xml).
Uporaba stanja mirovanja v aplikaciji Java:
V tem primeru bomo uporabili konfiguracijsko datoteko za mirovanje:
- Izjava tabele (Oracle)
CREATE TABLE CLIENT
(
ID_Client NUMBER (10) NOT NULL,
TITRE VARCHAR2 (10),
NOM VARCHAR2 (30),
OSTANI ŠTEVILO (19, 5),
ŠTEVILKA CA (19, 5),
CONSTRAINT PK_CLIENT PRIMARY KEY (CLIENT_ID)
);
- Izjava o razredih JAVA
/ ** constructeur par défaut * /
javni odjemalec () {
}
/ ** Konstrukcijski komplet ** /
javni odjemalec (Long clientId, String titer, String nom, BigDecimal remise, BigDecimal ca)
{
this.idClient = idClient;
this.titre = titer;
this.name = ime;
this.remise = remise;
this.ca = ca;
}
/ ** Les getters et setters ** /
javni Long getIdClient ()
{
vrni to.clientId;
}
public void setIdClient (dolg odjemalecId) {
this.clientId = clientId;
}
public String getTitre () {
vrni to.titre;
}
public void setTitre (titri)
this.titre = titer;
}
javni niz getNom () {
return this.nom;
}
public void setNom (String nom)
this.nom = nom;
}
javno BigDecimal getRemise () {
vrni to.
}
javni void setRemise (BigDecimal remise) {
this.remise = remise;
}
javno BigDecimal getCa () {
return this.ca;
}
javni void setCa (BigDecimal ca) {
this.ca = ca;
}
}
- Datoteke za kartiranje Client.hbm.xml:
"- // DTD 3.0 Hibernate / Hibernate Mapping
"//hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
name = "[Nom_Package] .Client" table = "CLIENT" entity-name = "Client" > @ hibernate.class table = "CLIENT" name = "idClient" type = "java.lang.Long" column = "ID_CLIENT" > Id stranke @ hibernate.id generator-class = "dodeljen" type = "java.lang.Long" column = "CLIENT_ID" name = "titre" type = "java.lang.String" column = "TITRE" length = "10" > Titre du client @ hibernate.property column = "TITRE" length = "10" name = "name" type = "java.lang.String" column = "NAME" length = "30" > Nom du client @ hibernate.property column = "NAME" length = "30" name = "remise" type = "java.math.BigDecimal" column = "ODPRAVI" length = "19" > Odpravi stranko @ hibernate.property column = "ODPRAVI" length = "19" name = "ca" type = "java.math.BigDecimal" column = "CA" length = "19" > Chiffre Affaire Primarni ključ je definiran v oznaki: v našem primeru temelji na zaporedju. S pomočjo oznake lahko določite sestavljeni ključ: composite-id> Spodnji primer uporablja metodo saveOrUpdate () v seji mirovanja. org.hibernate.Session sess = sessFact.openSession (); Stranka c = nova stranka (); c.setName ("John"); p.setTitre ("g."); Transakcija tx = sess.beginTransaction (); sess.saveOrUpdate (p); tx.commit (); sess.close ();