Insertion et exécution dans une table avec Hibernate et JPA
Bonjour,
Je veux insérer les données d'un objet java dans une table à l'aide d'hibernate et annotations JPA,dans ce cas j'ai crée deux classes java,les voici donc:
Employee.java
Code:
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
| package com.hibernate.chapter1;
import java.util.Date;
import java.util.Calendar;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
@Entity
@Table(name="EmployeeInfo")
public class Employee {
private int empId;
private String empName;
private String empPassword;
private String empEmailAddress;
private boolean isPermanent;
private Calendar empJoinDate;
private Date empLoginTime;
@Transient
public String getEmpPassword() {
return empPassword;
}
public void setEmpPassword(String empPassword) {
this.empPassword = empPassword;
}
@Column(nullable=false)
public String getEmpEmailAddress() {
return empEmailAddress;
}
public void setEmpEmailAddress(String empEmailAddress) {
this.empEmailAddress = empEmailAddress;
}
@Basic
public boolean isPermanent() {
return isPermanent;
}
public void setPermanent(boolean isPermanent) {
this.isPermanent = isPermanent;
}
@Temporal(TemporalType.DATE)
public Calendar getEmpJoinDate() {
return empJoinDate;
}
public void setEmpJoinDate(Calendar empJoinDate) {
this.empJoinDate = empJoinDate;
}
@Temporal(TemporalType.TIMESTAMP)
public Date getEmpLoginTime() {
return empLoginTime;
}
public void setEmpLoginTime(Date empLoginTime) {
this.empLoginTime = empLoginTime;
}
@Id
@Column(name="EmployeeId")
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
} |
et TestEmployee.java
Code:
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
| package com.hibernate.chapter1;
import java.sql.Date;
import java.util.GregorianCalendar;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class TestEmployee {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config= new AnnotationConfiguration();
config.addAnnotatedClass(Employee.class);
config.configure("hibernate.cfg.xml");
new SchemaExport(config).create(true, true);
SessionFactory factory = config.buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
Employee alex = new Employee();
alex.setEmpId(100);
alex.setEmpName("Eric eric");
alex.setEmpEmailAddress("eric@hibernate.com");
alex.setEmpPassword("eric");
alex.setPermanent(true);
alex.setEmpJoinDate(new GregorianCalendar(2013,7,5));
alex.setEmpLoginTime(Date.valueOf("2013-7-5"));
session.save(alex);
session.getTransaction().commit();
}
} |
et voici les problèmes et exceptions qui me donne durant l’exécution:
Citation:
Exception in thread "main" java.lang.IllegalArgumentException
at java.sql.Date.valueOf(Unknown Source)
at com.hibernate.chapter1.TestEmployee.main(TestEmployee.java:33)
et aussi il n'y a pas d'insertion dans la table
Merci pour votre aide