gpt4 book ai didi

spring - JpaRepository 不会保存数据

转载 作者:行者123 更新时间:2023-12-03 14:19:20 27 4
gpt4 key购买 nike

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

@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_ = user0_.uuid选择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_?

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

以下是我的 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;
}

}

最佳答案

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

关于spring - JpaRepository 不会保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32423746/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com