我正在使用Spring数据将我的数据保存到数据库中。我写了一个测试用例。它运行完美,但不会将数据插入数据库。
DatabaseTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={WebAppConfig.class, PersistenceConfig.class,
SecurityConfig.class, PropertyPlaceholderConfig.class})
@WebAppConfiguration
@Transactional
public class DatabaseTest {
@Autowired private UsersRepository usersRepository;
@Autowired private LoginsRepository loginsRepository;
@Test
public void saveOne2One(){
Logins lgn = new Logins();
lgn.setUserName("userName");
lgn.setPasswordHash("dfaskjdfaksjfdalksdf");
Users usr = new Users();
usr.setFirstName("test");
usr.setLastName("last");
usr.setUserName("userName");
usr.setLogins(lgn);
lgn.setUsers(usr);
loginsRepository.save(lgn);
//usersRepository.save(usr);
loginsRepository.flush();
}
}
Users.java
@Entity
@Table(name = "users", schema = "abcd")
public class Users implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 5074527544635474973L;
private Long userId;
private String firstName;
private String lastName;
private String userName;
private Logins logins;
public Users() {
}
public Users(Long userId) {
this.userId = userId;
}
public Users(Long userId, String firstName, String lastName,
String userName, Logins logins) {
this.userId = userId;
this.firstName = firstName;
this.lastName = lastName;
this.userName = userName;
this.logins = logins;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", nullable = false)
public Long getUserId() {
return this.userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
@Column(name = "first_name")
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "last_name")
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name = "user_name")
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Logins.java
@Entity
@Table(name = "logins", schema = "abcd")
public class Logins implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 3093763378845870599L;
private Long loginId;
private Users users;
private String userName;
//private String passwordSalt;
private String passwordHash;
public Logins() {
}
public Logins(Long loginId) {
this.loginId = loginId;
}
public Logins(Long loginId, Users users, String userName,
String passwordHash) {
this.loginId = loginId;
this.users = users;
this.userName = userName;
//this.passwordSalt = passwordSalt;
this.passwordHash = passwordHash;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "login_id", nullable = false)
public Long getLoginId() {
return this.loginId;
}
public void setLoginId(Long loginId) {
this.loginId = loginId;
}
@OneToOne(fetch = FetchType.LAZY, optional=false, cascade=CascadeType.ALL)
@JoinColumn(name = "related_user_id")
public Users getUsers() {
return this.users;
}
public void setUsers(Users users) {
this.users = users;
}
@Column(name = "user_name")
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "password_hash", length=60)
public String getPasswordHash() {
return this.passwordHash;
}
public void setPasswordHash(String passwordHash) {
this.passwordHash = passwordHash;
}
}
基础表是这样创建的:
abcd.logins
CREATE TABLE abcd.logins
(
login_id bigint,
user_name text COLLATE "default".pg_catalog,
password_salt text COLLATE "default".pg_catalog,
password_hash text COLLATE "default".pg_catalog,
related_user_id bigint
)
abcd.users
CREATE TABLE abcd.users
(
user_id bigint,
first_name text COLLATE "default".pg_catalog,
last_name text COLLATE "default".pg_catalog,
user_name text COLLATE "default".pg_catalog
)
我无法找出数据未持久存入数据库的原因。我甚至调用了flush()
存储库方法,但无济于事。有人可以帮帮我吗?
答案 0 :(得分:2)
如果您不想回滚交易,可以为特定测试添加@Rollback(false)。
在您的情况下,我希望您使用commit
方法提交了交易。
在你的情况下:
@Test
@Rollback(false)
public void saveOne2One(){
Logins lgn = new Logins();
lgn.setUserName("userName");
lgn.setPasswordHash("dfaskjdfaksjfdalksdf");
Users usr = new Users();
usr.setFirstName("test");
usr.setLastName("last");
usr.setUserName("userName");
usr.setLogins(lgn);
lgn.setUsers(usr);
loginsRepository.save(lgn);
//usersRepository.save(usr);
loginsRepository.flush();
}
或者如果你想在没有回滚的情况下为所有测试(类级别)保留数据,那么你可以使用:
@TransactionConfiguration(defaultRollback = false)
答案 1 :(得分:1)
您使用@Transaction执行junit Test,请参阅Spring Testing Transaction management,但默认情况下,框架将为每个测试创建并回滚事务。
如果您希望事务提交 - 异常,但偶尔需要特定测试来填充或修改数据库 - 可以指示TestContext框架使事务提交而不是通过@TransactionConfiguration回滚并且@Rollback注释。