我在Jboss AS 6.0.0 final上使用Hibernate 3.6.0和JPA 2.在我的EJB中,有一种方法可以更新实体值并对其进行一些查询.整个方法在BMT事务中运行.如果有任何失败,所有更改都应该回滚,而不是提交给DB.
数据库是mySql.
在运行JPA查询之前,JPA会自动将更改的状态刷新到DB,以防止任何过时的数据返回.但是,在我的方法中,自动刷新直接更新并将更改提交到DB,甚至之后出现了错误,更改不会回滚.所以我想问一下我的设置中是否有错误的配置,或者这是一个错误或什么的.
EJB
@Stateless(mappedName = "MyManagementBean")
@Local
@TransactionManagement(TransactionManagementType.BEAN)
public class MyManagement implements MyManagementLocal,MyManagementRemote {
@PersistenceUnit(unitName="MyEjb") EntityManagerFactory emf;
@Resource UserTransaction utx;
@Resource SessionContext ctx;
/**
* Default constructor.
*/
public MyManagement () {
// TODO Auto-generated constructor stub
}
public void dosomething(String id) throws Exception
{
try {
utx.begin();
em = emf.createEntityManager();
Myline line = em.find(Myline.class, id);
line.setStatus("R");
Stromg q += " from Myline as line ";
//auto flush apply here and directly committed to …Run Code Online (Sandbox Code Playgroud) hibernate transactions entitymanager hibernate-entitymanager jpa-2.0
我有一个与Event实体具有oneToMany关系的Project实体
public class Project {
....
@OneToMany(mappedBy = "dossier", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Event> events;
}
Run Code Online (Sandbox Code Playgroud)
我有一个ProjectService类
@Service
@Transactional
public class ProjectService {
public List<Project> findAll() {
return (List<Project>) projectRepository.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个ProjectController
@RestController
@RequestMapping(value = "/projects")
public class ProjectController {
@RequestMapping(method= RequestMethod.GET)
public List<Project> getAllProject() {
return projectService.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的客户端代码中,我看到项目的事件已加载,我不明白为什么.
我预计在DossierService中方法findAll的事务结束时,实体将被分离.显然,我的实体仍然附着,因为在我的控制器中的jackson序列化期间检索事件.
我将使用hibernate3的spring应用程序部署到glassfish服务器3.1.2时出现以下错误:
SEVERE: Exception while loading the app
SEVERE: log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository.
SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [file:/home/konrad/NetBeansProjects/webapp/experimental/SpringBar2012_1/SpringWebApp4/target/training/WEB-INF/classes/persistence-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: webappPersistenceUnit] Unable to build EntityManagerFactory
Run Code Online (Sandbox Code Playgroud)
这是我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.training.spring</groupId>
<artifactId>demoWebAppMVC</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>SpringWebAppMVC</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope> …Run Code Online (Sandbox Code Playgroud) java spring hibernate persistence.xml hibernate-entitymanager
如何确定对象是否已经存在,不会再次创建。
@Component
public class ProductServiceImpl implements ProductService
{
@PersistenceContext
private EntityManager em;
public Product getOrCreateProduct(String productName, String peoductDescr)
{
Product product =(new Product (productName, peoductDescr));
em.merge(product);
return product;
}
}
Run Code Online (Sandbox Code Playgroud)
我这样做是因为它仍然继续创建新的数据库条目,而不是返回新的数据库条目。
我有两个应用程序First Application将在while循环中持久存储到数据库中,循环将在很长一段时间后结束(例如10-15分钟).但是第二个应用程序需要第一个应用程序已经存储在数据库中的数据,第二个应用程序不能等待第一个应用程序完成.它将在第一个应用程序开始运行后启动. 我在第一个应用程序中使用了EntityManager.flush(),希望第一个应用程序能够立即将数据与数据库同步.这样,处于不同事务中的第二个应用程序就可以开始处理数据了.
这不起作用,flush()方法的目的是什么,以及如何解决我的问题?请帮忙 !!
我正在我的Repository类中扩展CrudRepository.我想使用findAll方法打印表格中的记录.到目前为止,我已经编写了一个测试类,我可以看到结果查询是正确的.如何在表格中打印单个记录?
这是我的代码片段:Repository Class
public interface RepositoryAda extends CrudRepository{
}
Run Code Online (Sandbox Code Playgroud)
服务类
@Service
public class Service{
@Autowired private RepositoryAda repository;
@Transactional
public List selectRecords(){
return (List) repository.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
测试用例:
@Test
public void getAllRecords() {
service.selectRecords();
}
Run Code Online (Sandbox Code Playgroud)
如何将表中的各个记录打印到控制台?