- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这里是main函数,已经给bean设置了值。
package demo.sphbIntegrate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App
{
public static void main( String[] args )
{
ApplicationContext context=new ClassPathXmlApplicationContext("sphb.xml");
EmployeeDAO edao=(EmployeeDAO) context.getBean("d");
Employee e=new Employee();
e.setId(1);
e.setName("sourav");
e.setSalary(100000);
edao.saveEmployee(e);
}
}
package demo.sphbIntegrate;
public class Employee
{
private int id;
private String name;
private int salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
这是我的 DAO 类。
package demo.sphbIntegrate;
import org.springframework.orm.hibernate5.HibernateTemplate;
public class EmployeeDAO
{
HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void saveEmployee(Employee e)
{
template.saveOrUpdate(e);
}
}
根据代码,该记录必须输入到表中。然而,发生了一些奇怪的事情,执行了一个选择查询,这在整个程序中没有提到。我无法理解这种异常现象。PS:我确信,我正在运行正确的程序,所有文件都正确保存,而且整个包中没有编写选择查询的代码。
这是输出:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select employee_.ID, employee_.NAME as NAME2_0_, employee_.SALARY as SALARY3_0_ from EMPLOYEE employee_ where employee_.ID=?
最佳答案
在这种情况下无需担心,因为 saveOrUpdate()
会同时执行保存和更新工作。
它首先检查您传递的 ID 是否存在于表中,原因与您看到选择查询的原因相同。如果您仔细观察您的查询
select employee_.ID, employee_.NAME as NAME2_0_, employee_.SALARY as SALARY3_0_ from EMPLOYEE employee_ where employee_.ID=?
您将在where条件下看到ID
。
如果此查询没有返回结果,则只会插入新记录,否则会发生更新。
关于java - hibernate 中的 saveOrUpdate() 不起作用,而是执行一个选择查询,该查询在程序中没有提到。 (使用spring+hibernate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54519128/
1. 引子 在项目开发过程中,有一些数据在写入时候,若已经存在,则覆盖即可。这样可以防止多次重复写入唯一键冲突报错。下面先给出两个MyBatis配置文件中使用saveOrUpdate的示例 ins
我有一个名为客户的 hibernate 实体,其中包含有关客户的信息。在我的方法中,我将客户详细信息作为参数获取,我需要将其插入数据库。因此,要删除重复条目,我会检查该客户是否已存在于数据库中。但问题
我尝试在 hibernate 中使用 saveorupdate 方法,结果是 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViola
我正在尝试做这样的方法,错误出现了并发问题..似乎我的更新方法没有增加我实体的@Version属性。 我的代码是这样的: @Transactional public B sav
saveOrUpdate() does the following: •if the object is already persistent in this session, do nothing
我尝试使用以下代码插入或更新数据库记录: Category category = new Category(); category.setName('catName'); category.setId
我正在通过以下链接使用 struts2 和 hibernate 集成 http://www.tutorials4u.net/struts2-tutorial/struts2_crud_example.
我正在尝试使用 Hibernate 插入或更新大数据。我有一个包含 350k 个对象的列表,当我使用 Hibernate saveOrUpdate() 时,需要几个小时才能插入所有数据。 我使用下面的
当使用 saveOrUpdate 创建一个新的 ebject 时,hibernate 将对象存储在数据库中并正确返回它。但是在同一方法调用中会创建一个带有一些空列的附加对象。 数据库中的对象如下所示:
@SuppressWarnings("unchecked") //@Override public SerialNumber getCounter(String id) { Session c
我的表有 2 个字段 id - 唯一、NotNull 和 Seq 名称 - Varchar 在空表上,我将记录发送为id = 1(手动填写id值) 名称 = '样本' 我使用 saveOrUpdate
我有 2 个双向映射的表,如下 @Entity @Table(name="testplan") public class TestPlan { @Id @Column(name="te
虽然我已经能够找到有关 Hibernate 事务如何工作的信息,因此数据库不会损坏,但更难理解 Hibernate 如何处理在线程之间共享的对象,并且每个线程都尝试将其保存到数据库。 这是我的理论问题
我有一个 Journal 类,它有一个 JournalLine 对象的 IList。 我创建了一个 Journal 并意外地通过相同的行生成方法运行了两次。此方法的开头调用 _journalLines
我正在尝试使用 Hibernate 中的 session.saveOrUpdate() 方法更新表格行。 但是,它无法更新该行并尝试通过生成插入语句来保存它。由于我的数据库中有一些不可为 null 的
我有一个名为 IssueParticipant 的 Hibernate 实体。它基本上描述了用户和问题(类似于 JIRA 或 Bugzilla 问题)之间的关系。它代表数据库中的一种多对多链接表,将用
我创建了一个名为“gauge_category”的表,它只有一个字段“gauge_category_id”是主键,数据类型是可变字符。我尝试了 CRUD 操作。创建、读取、删除操作完美运行。当我更新时
我正在使用 mysql 和 hibernate 在表中插入和更新行。我使用 saveOrUpdate 调用。现在,当我尝试更新表中的一行时,出现异常。异常指出我的列 requestTime 不能为空。
我正在使用 spring 来维护我应用程序中的事务。我想在数据库中保存新的并更新现有的用户详细信息。但是我无法更新对数据库的更改。查看我下面的代码并告诉我我的代码做错了什么? Session ses
我正在尝试使用父对象上的 saveOrUpdate 函数从 Hibernate Java 对象的集合中删除一个项目。更新和插入工作正常,但对象未正确删除。 saveOrUpdate() 是否能够识别和
我是一名优秀的程序员,十分优秀!