Ich habe eine User Entity
Beim Test
bekomme ich eine Fehlermeldung
Ich verstehe das Problem jetzt nicht so ganz. Kann mir jemand bitte bei der Lösung helfen?
Code:
package com.editor.entities;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.validator.constraints.Length;
import java.time.LocalDateTime;
import java.util.List;
@Entity
@Table(name="user")
@Inheritance( strategy = InheritanceType.TABLE_PER_CLASS )
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@CreationTimestamp
private LocalDateTime created_at;
@JoinColumn(name = "created_by", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private User created_by;
@OneToMany(mappedBy = "created_by", fetch = FetchType.LAZY)
private List<User> userList;
@UpdateTimestamp
private LocalDateTime updated_at;
// @OneToOne(cascade = CascadeType.REMOVE, fetch=FetchType.EAGER)
// @JoinColumn(name="updated_by")
// private User updated_by;
@NotBlank
@Length(min=3, max=255)
private String firstname;
@NotBlank
@Length(min=3, max=255)
private String lastname;
@NotBlank
@Length(min=3, max=255)
private String email;
@NotBlank
@Length(min=3, max=255)
private String password;
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDateTime getCreatedAt() {
return created_at;
}
public void setCreatedAt(LocalDateTime created_at) {
this.created_at = created_at;
}
public LocalDateTime getUpdatedAt() {
return updated_at;
}
public void setUpdatedAt(LocalDateTime updated_at) {
this.updated_at = updated_at;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public User getCreatedBy() {
return created_by;
}
public void setCreatedBy(User created_by) {
this.created_by = created_by;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((created_at == null) ? 0 : created_at.hashCode());
result = prime * result + ((created_by == null) ? 0 : created_by.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((lastname == null) ? 0 : lastname.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + ((updated_at == null) ? 0 : updated_at.hashCode());
result = prime * result + ((userList == null) ? 0 : userList.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (created_at == null) {
if (other.created_at != null)
return false;
} else if (!created_at.equals(other.created_at))
return false;
if (created_by == null) {
if (other.created_by != null)
return false;
} else if (!created_by.equals(other.created_by))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (firstname == null) {
if (other.firstname != null)
return false;
} else if (!firstname.equals(other.firstname))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (lastname == null) {
if (other.lastname != null)
return false;
} else if (!lastname.equals(other.lastname))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (updated_at == null) {
if (other.updated_at != null)
return false;
} else if (!updated_at.equals(other.updated_at))
return false;
if (userList == null) {
if (other.userList != null)
return false;
} else if (!userList.equals(other.userList))
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", created_at=" + created_at + ", updated_at=" + updated_at + ", firstname="
+ firstname + ", lastname=" + lastname + ", email=" + email + ", password=" + password + "]";
}
}
Beim Test
Code:
package com.editor.entities;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.metho.builder.repositories.UserRepository;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Optional;
@SpringBootTest
class UserTest {
@Autowired
UserRepository userRepository;
...
@Test
void testDeleteUserChild() {
String firstname1 = "michael1";
String lastname1 = "müller1";
String email1 = "michael.mueller.1@outlook.de";
String password1 = "password1";
String firstname2 = "michael2";
String lastname2 = "müller2";
String email2 = "michael.mueller.2@outlook.de";
String password2 = "password2";
User user1 = new User();
user1.setFirstname(firstname1);
user1.setLastname(lastname1);
user1.setEmail(email1);
user1.setPassword(password1);
User user2 = new User();
user2.setCreatedBy(user1);
user2.setFirstname(firstname2);
user2.setLastname(lastname2);
user2.setEmail(email2);
user2.setPassword(password2);
userRepository.save(user1);
userRepository.save(user2);
Optional<User> loadedUser1 = userRepository.findById(user1.getId());
assertTrue(loadedUser1.isPresent());
assertFalse(loadedUser1.get().getUserList().isEmpty(), loadedUser1.get().getUserList().toString());
}
/**
* clean up
*/
@AfterEach
public void cleanUp() {
userRepository.deleteAll();
}
}
bekomme ich eine Fehlermeldung
Ich verstehe das Problem jetzt nicht so ganz. Kann mir jemand bitte bei der Lösung helfen?