Springデータを使用してデータをデータベースに保存しています。私はテストケースを書いた。これは完全に実行されますが、データをデータベースに挿入しません。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()
リポジトリのメソッドを呼び出すが、役に立たない。誰でも助けてくれますか?