S_O_T 2016-09-17 15:29 采纳率: 0%
浏览 2163

jpa+em.persist(stu)不能把stu对象持久化到数据中,程序不报错,但可以查数据

请各位大神帮忙解决一个问题:
spring+jpa+em.persist(student)不能把student对象持久化到Mysql数据中,程序运行也不报错,但可以查到数据库中原有数据,这是什么原因啊?谢谢!(我是新手,对jpa还不太熟悉)

spring配置:

 <?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:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        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.1.xsd">

    <bean id="jdbc"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations" value="classpath:/dev/jdbc.properties"></property>
    </bean>
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="#{jdbc.driverClassName}"></property>
        <constructor-arg value="#{jdbc.url}"></constructor-arg>
        <constructor-arg value="#{jdbc.userName}"></constructor-arg>
        <constructor-arg value="#{jdbc.password}"></constructor-arg>
    </bean>
    <bean id="emf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <description>
            ..........
        </description>
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="default" />
        <property name="packagesToScan">
            <list>
                <value>com.wyiwei.entity_modle.infrastructur.shared.modle</value>
            </list>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true" />
                <property name="showSql" value="false" />
            </bean>
        </property>
    </bean>


<!--     <bean class="org.springframework.jdbc.core.JdbcTemplate"> -->
<!--         <property name="dataSource" ref="dataSource" /> -->
<!--     </bean> -->

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

<!--     <tx:annotation-driven  -->
<!--         transaction-manager="transactionManager" proxy-target-class="true"/> -->
    <tx:annotation-driven mode="aspectj"
        transaction-manager="transactionManager" />
</beans>

实体类:

 @Entity
@Table(name="STUDENT")
public class Student implements Serializable{

    private static final long serialVersionUID = -4022855296927067776L;

    @Id
    @Column(name="STU_SEQ", nullable=false)
    private Integer stuSeq;

    @Column(name="STUDENT_NAME", nullable=false, length=21)
    private String studentName;

    @Column(name="STU_AGE", nullable=false, length=4)
    private Integer stuAge;

    public Integer getStuSeq() {
        return stuSeq;
    }

    public void setStuSeq(Integer stuSeq) {
        this.stuSeq = stuSeq;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Integer getStuAge() {
        return stuAge;
    }

    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }

    @Override
    public String toString() {
        return "Student [stuSeq=" + stuSeq + ", studentName=" + studentName
                + ", stuAge=" + stuAge + "]";
    }
}

测试代码:
@ContextConfiguration({ "/test-context.xml"})
public class PersistTest extends AbstractTest{

@PersistenceContext
private EntityManager em;

@Test
public void test(){

// query();

    persist();

    System.out.println("Over!");
}

@Transactional
private void persist(){
    Student s = new Student();

    s.setStuAge(30);
    s.setStudentName("张三");
    s.setStuSeq(1);

    em.persist(s);
}

@Transactional
private void query(){
    QStudent q = QStudent.student;

    Student student = new JPAQuery(em).from(q).where(q.stuSeq.eq(1)).uniqueResult(q);

    System.out.println(student);


}
  • 写回答

1条回答 默认 最新

  • S_O_T 2016-09-19 14:12
    关注

    问题解决了!原来还是因为事务的问题,spring配置了事务,代码里也有@Transactional注解,可还是事务不能提交。这是为什么呢?原来在同一个
    类中,方法a调用加了@Transactional注解的方法b,方法b的事务是不起作用的。(另一个类调用b方法会有事务)

    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条