javax.persistence.TransactionRequiredException

30 浏览
0 Comments

javax.persistence.TransactionRequiredException

我遇到了以下错误,我已经查看了所有类似的问题,但无法确定我出错的地方。

以下是错误信息:

Exception in thread "main" javax.persistence.TransactionRequiredException: No transactional EntityManager available
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:275)
    at com.sun.proxy.$Proxy19.persist(Unknown Source)
    at com.project.vibhas.dao.hibernate.GenericDaoJpa.save(GenericDaoJpa.java:45)
    at com.project.vibhas.service.impl.PersonServiceImpl.save(PersonServiceImpl.java:22)
    at com.project.vibhas.domain.App.main(App.java:25)

这是我的代码:

package com.project.vibhas.dao.hibernate;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;
import com.project.vibhas.dao.GenericDao;
import com.project.vibhas.domain.DomainObject;
public class GenericDaoJpa implements GenericDao {
    private Class type;
    protected EntityManager entityManager;
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    public GenericDaoJpa(Class type) {
        super();
        this.type = type;
    }
    @Transactional
    public void save(T object) {
        System.out.println("inside generic dao");
            entityManager.persist(object);
    }
    public void delete(T object) {
        entityManager.remove(object);
    }
}

我的Dao类:

@Repository("personDao")
public class PersonDaoJpa extends GenericDaoJpa implements PersonDao {
    public PersonDaoJpa() {
        super(Person.class);
    }
    public Person authenticatePerson(String username, String password)
            throws DataAccessException, AuthenticationException {
        List results = null;
        Query query = entityManager
                .createQuery("from Person as p where p.username = :username and p.password = :password");
        query.setParameter("username", username);
        query.setParameter("password", password);
        results = query.getResultList();
        if (results == null || results.size() <= 0) {
            throw new AuthenticationException("No users found");
        } else {
            return results.get(0);
        }
    }
    public Person getPersonByUsername(String username)
            throws DataAccessException, EntityNotFoundException {
        List results = null;
        Query query = entityManager
                .createQuery("from Person as p where p.username = :username");
        query.setParameter("username", username);
        results = query.getResultList();
        if (results == null || results.size() <= 0) {
            throw new EntityNotFoundException(username + " not found");
        } else {
            return results.get(0);
        }
    }
}

我的服务类:

package com.project.vibhas.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.project.vibhas.dao.PersonDao;
import com.project.vibhas.domain.Person;
import com.project.vibhas.service.PersonService;
@Service("personService")
public class PersonServiceImpl implements PersonService{
    @Autowired
    PersonDao personDao;
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
    public void save(Person person) {
        System.out.println("Inside service");
        //personDao.get(1L);
        personDao.save(person);
    }
}

我的测试类:

package com.project.vibhas.domain;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.project.vibhas.service.PersonService;
/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext appContext = new ClassPathXmlApplicationContext(
                "classpath*:META-INF/spring/spring-jpa.xml");
        PersonService personService = (PersonService) appContext.getBean("personService");
        Person person = new Person();
        person.setId(1L);
        personService.save(person);
        System.out.println("Done");
    }
}

Spring-Jpa.xml



    
    
    
     
    
    
    
    

Persistence.xml



org.hibernate.ejb.HibernatePersistence








0