[英]Transaction Exception in hibernate
I'm trying to do simple hibernate saving data to my table but I have Transaction Exception in the console, here is my code : 我正在尝试做简单的将数据保存到表的休眠方式,但是控制台中有事务异常,这是我的代码:
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;
}
and that's my insertion method: 那就是我的插入方法:
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();
}
my loginServlet where i'm saving my data : 我的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();
}
my table in DB 我的数据库表
create table Logging(
id int not null,
user_name varchar(50) not null,
action varchar(50) not null,
Date date
primary key (id)
);
another issue that appears in the console the hibernate print that the data is inserted to the table but with wrong order like that 控制台中出现的另一个问题是将数据插入表但顺序错误的休眠打印
Hibernate: insert into Logging (action, DATE, USER_NAME, ID) values (?, ?, ?, ?)
and it should be like that 它应该是这样的
Hibernate: insert into Logging (id, user_name, action, date) values (?, ?, ?, ?)
when i check the table i found it empty ?? 当我检查表时,发现它是空的?
There is a difference in the column name specified in the query to create the table and in the Logging.java file. 在创建表的查询和Logging.java文件中指定的列名有所不同。 That's why your console shows
这就是为什么您的控制台显示
_Hibernate: insert into Logging (action, DATE, USER_NAME, ID) values (?, ?, ?, ?)_
instead of _Hibernate: insert into Logging (id, user_name, action, date) values (?, ?, ?, ?)_ 而不是_Hibernate:插入到Logging(ID,用户名,操作,日期)值(?,?,?,?)_
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.