1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Foo {
protected int id;
protected String name;
public Foo() {
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
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;
}
@Override
public String toString() {
return "Foo [id=" + id + ", name=" + name + ", getId()=" + getId()
+ ", getName()=" + getName() + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + ", toString()="
+ super.toString() + "]";
}
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
public class TestFoo {
public static void main(String[] args) {
AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration
.setProperty("hibernate.dialect",
"org.hibernate.dialect.HSQLDialect")
.setProperty("hibernate.connection.driver_class",
"org.hsqldb.jdbcDriver")
.setProperty("hibernate.connection.url",
"jdbc:hsqldb:file:local/foo.hsql")
.setProperty("hibernate.connection.username", "sa")
.setProperty("hibernate.connection.password", "");
configuration.setProperty("hibernate.connection.pool_size", "4");
configuration.setProperty("hibernate.connection.autocommit", "true");
configuration.setProperty("hibernate.cache.provider_class",
"org.hibernate.cache.NoCacheProvider");
configuration.setProperty("hibernate.transaction.factory_class",
"org.hibernate.transaction.JDBCTransactionFactory");
configuration.setProperty("hibernate.current_session_context_class",
"thread");
new SchemaUpdate(configuration);
configuration.addAnnotatedClass(Foo.class);
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Foo foo = new Foo();
foo.setName("Une autre foo");
session.persist(foo);
transaction.commit();
session.close();
}
} |
Partager