Question

I am facing a problem, when I tried to insert a data into Database through JPA (@persistanceContex)

Observations

  1. Not getting any errors;
  2. Record is not storing into database (save)
  3. When I tried with listAll() ; it retrieving the data from database

Domain

@Entity
public class Test {

    @Id
    private int id;

    @Column(name="full_name")
    private String fullName;

    @Column(name="mobile_number")
    private int mobileNumber;

    .....

}

DAO Class

@Repository("testDAO")
@Transactional
public class TestDAO {


    private EntityManager entityManager;

     @PersistenceContext(unitName="CRUD_Test_Annotation")
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public void save(Test test){
        entityManager.persist(test);
    }
}

Service

@Service("testService")
@Transactional
public class TestService {

    private static final Logger logger = LoggerFactory.getLogger(TestService.class);

    @Autowired(required=true)
    private TestDAO testDAO;

    public void save(Test test){
        logger.info("TestService::save()");
        testDAO.save(test);
    }

    public void list(){
        testDAO.getAll();
    }

}

Controller

@RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Locale locale, Model model) {

        Test test = new Test();
        test.setId(xx);
        test.setFullName("xxxxx");
        test.setMobileNumber(yyyyyy);

        testService.save(test);
        return "home";
    }

application-context.xml

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

    <!-- Declare a JPA entityManagerFactory-->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
        <property name="persistenceUnitName" value="CRUD_Test_Annotation" />

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

    <!-- Declare a transaction manager-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
Was it helpful?

Solution

First of all, you don't need two transaction boundaries, I suggest you remove @Transactional from your DAO and keep the one in your service.

Start by verifying that spring-transaction has initiated a transaction: Use the debugger and stop the application after the transaction boundary, for instance in your TestService.save-method. If transactions are running, you will see org.springframework.transaction.interceptor.TransactionInterceptor#invoke in the call stack. If you don't see the TransactionInterceptor, then that's your problem. Post your persistence.xml file if transactions are running.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top