JpaRepository不会保存数据

时间:2015-09-06 12:49:36

标签: spring hibernate jpa

我使用JpaRepository来保存数据,但是hibernate.show_sql显示" select"并且不会保存数据。以下是我的服务:

@Autowired
private UserRepository userRepository;

@PostConstruct
public void init() {
    User admin = new User();
    admin.setDisplayName("admin");
    admin.setEmailAddress("admin@admin");
    admin.setPassword("admin___");
    admin.setRegisteredAt(new Date());
    admin.setLastAccessAt(new Date());
    admin.setUuid(UUID.randomUUID().toString());

    try {
        System.out.println("before save");
        userRepository.save(admin);
        System.out.println("after save");
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

输出如下:

========保存之前======

Hibernate:选择user0_.uuid为uuid1_0_0_,user0_.display_name为display_2_0_0_,user0_.email_address为email_ad3_0_0_,user0_.last_access_at为last_acc4_0_0_,user0_.password为password5_0_0_,user0_.registered_at为register6_0_0_来自用户user0_,其中user0_.uuid =?

========保存后=======

以下是我的applicationContext.xml:

<context:component-scan base-package="test">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/helloworld" />
    <property name="username" value="root" />
    <property name="password" value="password" />
</bean>

<bean id="myEmf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="test.entity"></property>
    <property name="dataSource" ref="myDataSource" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="persistenceProvider">
        <bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
    </property>
</bean>

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

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="myDataSource" />
</bean>

<jpa:repositories base-package="test.repository"
    entity-manager-factory-ref="myEmf" transaction-manager-ref="transactionManager"></jpa:repositories>

附件是由JPA Tools生成的我的类:

@Entity
@NamedQuery(name="User.findAll", query="SELECT u FROM User u")
public class User implements Serializable {
  private static final long serialVersionUID = 1L;

  @Id
  private String uuid;

  @Column(name="display_name")
  private String displayName;

  @Column(name="email_address")
  private String emailAddress;

  @Temporal(TemporalType.TIMESTAMP)
  @Column(name="last_access_at")
  private Date lastAccessAt;

  private String password;

  @Temporal(TemporalType.TIMESTAMP)
  @Column(name="registered_at")
  private Date registeredAt;

  public   User() {
  }

  public String getUuid() {
      return this.uuid;
  }

  public void setUuid(String uuid) {
      this.uuid = uuid;
  }

  public String getDisplayName() {
      return this.displayName;
  }

  public void setDisplayName(String displayName) {
      this.displayName = displayName;
  }

  public String getEmailAddress() {
      return this.emailAddress;
  }

  public void setEmailAddress(String emailAddress) {
      this.emailAddress = emailAddress;
  }

  public Date getLastAccessAt() {
      return this.lastAccessAt;
  }

  public void setLastAccessAt(Date lastAccessAt) {
      this.lastAccessAt = lastAccessAt;
  }

  public String getPassword() {
      return this.password;
  }

  public void setPassword(String password) {
      this.password = password;
  }

  public Date getRegisteredAt() {
      return this.registeredAt;
  }

  public void setRegisteredAt(Date registeredAt) {
      this.registeredAt = registeredAt;
  }

}

1 个答案:

答案 0 :(得分:2)

由于您使用的是JPA,因此事务管理器应该是JpaTransactionManager,而不是DataSourceTransactionManager