Ok,
This my Adresse class :
Code:
package model.hibernate;
import java.io.Serializable;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
@Entity
@NamedQuery(name="adresse.all", query="FROM Adresse")
public class Adresse implements Serializable {
private int id;
@Transient
private String adresse;
@Transient
private String codePostal;
@Transient
private String ville;
public Adresse() {
}
public String getAdresse() {
return adresse;
}
public String getCodePostal() {
return codePostal;
}
/**
*
* @return
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return id;
}
public String getVille() {
return ville;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public void setCodePostal(String codePostal) {
this.codePostal = codePostal;
}
private void setId(int id) {
this.id = id;
}
public void setVille(String ville) {
this.ville = ville;
}
}
This is my HibernateUtil class :
Code:
package control.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import model.hibernate.Adresse;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Configuration annotationConfiguration = new AnnotationConfiguration()
//.configure("hibernate.cfg.xml");
//.addFile("src/hibernate.cfg.xml")
.addPackage("model.hibernate"); // le nom complet du package
//.addAnnotatedClass(Adresse.class);
// .addResource("test/animals/orm.xml")*/
sessionFactory = annotationConfiguration.configure().buildSessionFactory();
// to test the file on export because it is empty...
SchemaExport export = new SchemaExport(annotationConfiguration);
export.setOutputFile("test");
export.create(true, true);
export.drop(true, true);
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException {
return sessionFactory.openSession();
}
}
This is my main code :
Code:
Query q = HibernateUtil.getSession().getNamedQuery("adresse.all");
List results = q.list();
System.out.println(results);
Session session = HibernateUtil.getSession();
Adresse adresse = new Adresse();
Transaction transaction = session.beginTransaction();
session.save(adresse);
transaction.commit();
session.close();
This is my hibernates.properties files :
Code:
hibernate.dialect org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class org.postgresql.Driver
hibernate.connection.url jdbc:postgresql:MyDB
hibernate.connection.username postgres
hibernate.connection.password bla
hibernate.connexion.pool_size 1
hibernate.show_sql true
hibernate.hbm2ddl.auto create-drop
This is my hibernate.cfg.xml file :
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.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql:MyDB</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">bla</property>
<property name="hibernate.connexion.pool_size">1</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
</session-factory>
</hibernate-configuration>
And I think that all...