Bonjour,
J'ai le code suivant:
La classe Employee:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 public class Example { public static void main(String[] args) throws Exception { SessionFactory session = HibernateUtil.getSessionFactory(); Session sess = session.getCurrentSession(); Transaction tx = sess.beginTransaction(); Employee pojo = new Employee(); pojo.setId(new Integer(5)); pojo.setName("XYZ"); tx.commit(); System.out.println("Record inserted"); session.close(); } }
et hibernate.cfg.xml:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 @Entity @Table (name = "employee") public class Employee implements Serializable{ @Id @Column(name = "id") Integer id; @Column(name = "name") String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Ce qu'il y a c'est que quand j'exécute le programme, je regarde dans ma base de données, mais il n'y aucune entrée qui est rajoutée dans la table Employee. J'ai oublié quelque chose?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?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="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test_hibernate</property> <property name="connection.username">root</property> <property name="connection.password"></property> <property name="connection.pool_size">1</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto"></property> <mapping class="com.descartes.model.Employee" /> </session-factory> </hibernate-configuration>
Partager