Java 使用Hibernate SessionFactory插入不起作用的值
我在SpringMVC中使用Hibernate作为ORMJava 使用Hibernate SessionFactory插入不起作用的值,java,hibernate,jdbc,Java,Hibernate,Jdbc,我在SpringMVC中使用Hibernate作为ORM String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)" + " values (?,?,?)"; Connection connections = sessionFactory.getCurrentSession().connectio
String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)"
+ " values (?,?,?)";
Connection connections = sessionFactory.getCurrentSession().connection();
PreparedStatement stmt = connections.prepareStatement(querys);
stmt.setString(1, "Auto");
stmt.setInt(2, 1);
stmt.setInt(3, 251);
stmt.executeUpdate();;
connections.close();
在我的模块中,我调用一个表单操作来将值存储到表中。但数据并没有插入到该表中。但我的日志中没有任何错误
String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)"
+ " values (?,?,?)";
Connection connections = sessionFactory.getCurrentSession().connection();
PreparedStatement stmt = connections.prepareStatement(querys);
stmt.setString(1, "Auto");
stmt.setInt(2, 1);
stmt.setInt(3, 251);
stmt.executeUpdate();;
connections.close();
我用sessionFactory试过的代码是
String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)"
+ " values (?,?,?)";
Connection connections = sessionFactory.getCurrentSession().connection();
PreparedStatement stmt = connections.prepareStatement(querys);
stmt.setString(1, "Auto");
stmt.setInt(2, 1);
stmt.setInt(3, 251);
stmt.executeUpdate();;
connections.close();
但同样,我可以使用JDBC驱动程序插入值,如下所示
String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)"
+ " values (?,?,?)";
Connection connections = sessionFactory.getCurrentSession().connection();
PreparedStatement stmt = connections.prepareStatement(querys);
stmt.setString(1, "Auto");
stmt.setInt(2, 1);
stmt.setInt(3, 251);
stmt.executeUpdate();;
connections.close();
private static final String DB_DRIVER = "org.postgresql.Driver";
private static final String DB_CONNECTION = "jdbc:postgresql://localhost:5432/xxxxx";
private static final String DB_USER = "xxxxxx";
private static final String DB_PASSWORD = "xxxxx";
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String querys = "INSERT INTO reconcile_process "
+ "(process_type,fk_last_modified_by,fk_bank_stmt_id) VALUES"
+ "(?,?,?)";
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(querys);
preparedStatement.setString(1, "Type");
preparedStatement.setLong(2, 45);
preparedStatement.setLong(3, 251);
preparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
private static Connection getDBConnection() {
Connection dbConnection = null;
try {
Class.forName(DB_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
DB_PASSWORD);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
有人能告诉我问题出在哪里吗?如果您正在使用Hibernate,为什么不利用它的优势呢
String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)"
+ " values (?,?,?)";
Connection connections = sessionFactory.getCurrentSession().connection();
PreparedStatement stmt = connections.prepareStatement(querys);
stmt.setString(1, "Auto");
stmt.setInt(2, 1);
stmt.setInt(3, 251);
stmt.executeUpdate();;
connections.close();
SessionFactory sessionFactory =
new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
txn = session.beginTransaction();
UrTableClass obj = new UrTableClass ();
obj.setDescription("Description");
obj.setName("NAME");
obj.setAge(28);
session.save(obj);
txn.commit();
请在默认包中签入hibernate.cfg.xml文件
String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)"
+ " values (?,?,?)";
Connection connections = sessionFactory.getCurrentSession().connection();
PreparedStatement stmt = connections.prepareStatement(querys);
stmt.setString(1, "Auto");
stmt.setInt(2, 1);
stmt.setInt(3, 251);
stmt.executeUpdate();;
connections.close();
<property name="hibernate.connection.autocommit">true</property>
为了找到问题的根本原因,我建议在
debug
模式下启动服务器。然后使用F6
逐行调试。这将帮助您跟踪在对象中传递的值。
如果您一直在使用hibernate,我强烈感觉您遗漏了hibernate.cfg.xml
中通过hibernate
保存到数据库中所需的一些重要配置,如果您遇到问题,请显示错误。我在该表中有一个数组类型列。。所以我决定使用Hibernate进行本机查询。。无法使用hibernate表中的数组列…即使这样也很简单,但这一切都取决于您如何设置以及您现在有多少时间。我有足够的时间。。。我对在实体类中获取数组感到困惑,也没有关于如何使用Hibernate将数组插入postgres的明确参考。这两种技术都适用于基于注释的Hibernate。当您从数据库执行反向工程时,将生成Hibernate.cfg.xml填充,并将数据库用户名和密码存储在其中,只需添加true即可避免重复txn.commit()的行。true已修复此问题。。。设置此属性是否会影响任何其他事务?因为,它被设置为false..然后在每个事务之后执行提交操作,正如我在应答SessionFactory SessionFactory=new Configuration().configure().buildSessionFactory()中解释的那样;session=sessionFactory.openSession();txn=session.beginTransaction();提交();
String querys = "insert into reconcile_process (process_type,fk_last_modified_by,fk_bank_stmt_id)"
+ " values (?,?,?)";
Connection connections = sessionFactory.getCurrentSession().connection();
PreparedStatement stmt = connections.prepareStatement(querys);
stmt.setString(1, "Auto");
stmt.setInt(2, 1);
stmt.setInt(3, 251);
stmt.executeUpdate();;
connections.close();