0
我試圖做簡單的休眠數據保存到我的表,但是我在控制檯交易異常,這裏是我的代碼:交易異常
logging.java
@Entity
@Table(name = "Logging")
public class Logging implements Serializable {
private int id;
private String user;
private String action;
private Date date;
@Column(name = "ID")
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "USER_NAME")
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
@Column(name = "action")
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
@Column(name = "DATE")
@Temporal(TemporalType.DATE)
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
,這就是我的插入方法:
public class BaseDAO {
public SessionFactory factory;
public Session session;
public BaseDAO() {
factory = new AnnotationConfiguration().configure(
"hibernate.cfg.xml").buildSessionFactory();
}
public void save(Object obj) throws Throwable {
session = factory.openSession();
Transaction tx = null;
tx = session.beginTransaction();
session.save(obj);
tx.commit();
}
我loginServlet在那裏我救了我的數據:
Logging loggingdto = new Logging();
try {
loggingdto.setId(1);
loggingdto.setUser(loggedInUserDTO.getUserName());
loggingdto.setAction("USER LOGGED IN");
loggingdto.setDate(new Date());
base.save(loggingdto);
} catch (Throwable e) {
e.printStackTrace();
}
我的表DB
create table Logging(
id int not null,
user_name varchar(50) not null,
action varchar(50) not null,
Date date
primary key (id)
);
,在控制檯中出現的數據插入到表,但有錯誤的順序一樣,
Hibernate: insert into Logging (action, DATE, USER_NAME, ID) values (?, ?, ?, ?)
,它應該是這樣的
Hibernate: insert into Logging (id, user_name, action, date) values (?, ?, ?, ?)
休眠打印另一個問題
當我檢查表我發現它是空的?
加入交易異常堆棧跟蹤,請。 –
另外,添加你的'hibernate.cfg.xml'文件! –
@ N00b Pr0grammer我相信我的hibernate.cfg.xml是正確的 –