gpt4 book ai didi

java - JPA find() 有效,坚持无异常无结果,删除显示查询并抛出删除分离实例异常

转载 作者:太空宇宙 更新时间:2023-11-04 06:09:31 25 4
gpt4 key购买 nike

我不知道应该添加什么才能使其正常工作。这是我的实体类:

@Entity
public class Student {

@Id
@GeneratedValue
private int id;
private String name;
private int age;

//setters and getters
}

我的application-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config />
<context:component-scan base-package="orm" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="wisla666"/>
</bean>

<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="orm.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
</bean>

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf" />
</bean>

<tx:annotation-driven transaction-manager="txManager"/>

这是我的 Dao 类:

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import orm.entity.Student;

@Repository
public class StudentDaoImpl implements StudentDao {

@PersistenceContext
private EntityManager entityManager;

@Override
public Student loadStudent(int id) {
return entityManager.find(Student.class, id);
}

@Override
public void saveStudent(Student student) {
entityManager.persist(student);
}

@Override
public void deleteStudent(Student student) {
entityManager.remove(entityManager.merge(student));
}
}

首先,我从数据库加载 Student 对象,它可以工作,然后我尝试从数据库中删除该对象,但它不起作用。在删除期间,sql 语法可见:

Hibernate: select student0_.id as id1_0_0_, student0_.age as age2_0_0_, student0_.name as name3_0_0_ from Student student0_ where student0_.id=?

并且抛出异常:

java.lang.IllegalArgumentException: Removing a detached instance orm.entity.Student#2

在持久化 Student 对象期间,没有任何反应。

问题是我该怎么做才能让它发挥作用?

最佳答案

代码:

@Repository
public class StudentDaoImpl implements StudentDao {

@PersistenceContext
private EntityManager entityManager;

@Transactional
@Override
public Student loadStudent(int id) {
return entityManager.find(Student.class, id);
}
}

主要方法:

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
StudentServiceImpl ssi = context.getBean("studentServiceImpl", StudentServiceImpl.class);
}

异常:创建名为“studentServiceImpl”的 bean 时出错: Autowiring 依赖项注入(inject)失败

这是我的问题:@Transactional 是否不能与 @PersistenceContext 位于同一位置?为什么我不能分配这个 bean?

关于java - JPA find() 有效,坚持无异常无结果,删除显示查询并抛出删除分离实例异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28906863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com