Spring @Transactional没有插入表中

时间:2016-01-21 11:24:13

标签: java mysql spring hibernate

我无法将数据保存到数据库中。我可以在日志中看到插入SQL语句,并且还将生成的ID写入日志。我面临的问题是数据没有从会话中持久存储到数据库中。 从数据库中获取数据时我没有遇到任何问题。我使用的用户具有对数据库的读/写访问权限,而且我也没有收到任何错误。

我使用的是Spring 4.2.3,Hibernate 5.5.0,MySQL 5和Primefaces 5.3。

我搜索所有论坛,发现没有任何帮助我。 有人遇到过类似的问题吗?

模型

@Entity
@Table(name = "borrower", uniqueConstraints = {
        @UniqueConstraint(columnNames = "borrower_id")
})
public class Borrower implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "borrower_id", unique = true, nullable = false)
    private Integer borrowerId;
    @Column(name = "borrower_code", nullable = true, length = 20)
    private String borrowerCode;
    @Column(name = "first_name", nullable = false, length = 45)
    private String firstName;
    @Column(name = "last_name", nullable = true, length = 45)
    private String lastName;
    @Column(name = "phone", nullable = true, length = 45)
    private String phone;
    @Column(name = "borrower_status", nullable = false, length = 15)
    private String borrowerStatus;
    @Column(name = "email_id", nullable = false, length = 200)
    private String emailId;

    public Borrower() {
    }

    // Getters and setters
}

DAO

public class BorrowerDAOImpl extends HibernateDaoSupport implements BorrowerDAO {

    private final Logger logger = LogManager.getLogger(getClass());
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public boolean saveBorrower(Borrower borrower) {
        try {
            Session session = sessionFactory.getCurrentSession();
            session.persist(borrower);
            session.flush();
            logger.info("Borrower '{} {}' with Borrower ID '{}' saved successfully.", borrower.getFirstName(), borrower.getLastName(), borrower.getBorrowerId());
            session.disconnect();
            return true;
        }
        catch (Exception e) {
            logger.error("Unable to add new borrower '{} {}', error: ", borrower.getFirstName(), borrower.getLastName(), e);
            return false;
        }
    }
}

服务

@Service("borrowerBO")
@SessionScoped
@Transactional(readOnly = true)
public class BorrowerBOImpl implements BorrowerBO {

    @Autowired
    BorrowerDAO borrowerDAO;

    @Transactional(readOnly = false)
    @Override
    public boolean saveBorrower(Borrower borrower) {
        return borrowerDAO.saveBorrower(borrower);
    }

    public BorrowerDAO getBorrowerDAO() {
        return borrowerDAO;
    }

    public void setBorrowerDAO(BorrowerDAO borrowerDAO) {
        this.borrowerDAO = borrowerDAO;
    }
}

Spring Config

<?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-4.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.movielibrary.bo"/>

    <!-- SessionFactory bean -->
    <import resource="classpath:HibernateSessionFactory.xml"/>

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

    <!-- Transaction manager bean -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- Bean declaration -->
    <bean id="borrowerBO" class="com.movielibrary.bo.impl.BorrowerBOImpl">
        <property name="borrowerDAO" ref="borrowerDAO" />
    </bean>
    <bean id="borrowerDAO" class="com.movielibrary.dao.impl.BorrowerDAOImpl">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

休眠配置

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

    <!-- Database configuration bean -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/test_db?relaxAutoCommit=true" />
        <property name="username" value="user" />
        <property name="password" value="password" />
    </bean>

    <!-- SessionFactory bean -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- Hibernate annotations property -->
        <property name="packagesToScan" value="com.movielibrary.model" />   
        <property name="annotatedClasses">
            <list>
                <value>com.movielibrary.model.Borrower</value>
            </list>
        </property>
    </bean>
</beans>

0 个答案:

没有答案