我正在使用Spring Boot + JPA保存某些实体的应用程序。但是,这些实体(当然还有Spring配置)是应用程序中唯一依赖Spring / JPA的东西。这就是为什么我们想将应用程序分为两个部分。应该独立于Spring和任何JPA相关事物的核心应用程序以及Spring应用程序。为此,我们在核心应用程序中创建了实体的接口和基础实现,在Spring应用程序中创建了派生实现,该实现仅覆盖getter并使用属性访问。但是现在Spring抱怨说这些字段不存在。
这是核心应用程序中的类,基本上只是一堆getter和setter:
public class UserImpl implements User {
protected Set<UserProfile> userProfiles = new HashSet<>();
protected Set<Playlist> playlists = new HashSet<>();
protected String userName;
protected String password;
protected String securityQuestion;
protected String securityQuestionAnswer;
protected String token;
public UserImpl() {
}
public UserImpl(String userName) {
this.userName = userName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSecurityQuestion() {
return securityQuestion;
}
public void setSecurityQuestion(String securityQuestion) {
this.securityQuestion = securityQuestion;
}
public String getSecurityQuestionAnswer() {
return securityQuestionAnswer;
}
public void setSecurityQuestionAnswer(String securityQuestionAnswer) {
this.securityQuestionAnswer = securityQuestionAnswer;
}
public void setUserProfiles(Set<UserProfile> userProfiles) {
this.userProfiles = userProfiles;
}
public List<UserProfile> getUserProfiles() {
return new ArrayList<>(userProfiles);
}
public void setToken(String token) {
this.token = token;
}
public String getToken() {
return token;
}
public void setPlaylists(Set<Playlist> playlists) {
this.playlists = playlists;
}
public Set<Playlist> getPlaylists() {
return playlists;
}
}
这是派生类:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "users", schema = "musictinder")
public class JPAUserImpl extends UserImpl {
@Id @GeneratedValue private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Override
@Column(name = "user_name")
@AccessType(AccessType.Type.PROPERTY)
public String getUserName() {
return super.getUserName();
}
@Override
@Column(name = "password")
@AccessType(AccessType.Type.PROPERTY)
public String getPassword() {
return super.getPassword();
}
@Override
@Column(name = "security_question")
@AccessType(AccessType.Type.PROPERTY)
public String getSecurityQuestion() {
return super.getSecurityQuestion();
}
@Override
@Column(name = "security_question_answer")
@AccessType(AccessType.Type.PROPERTY)
public String getSecurityQuestionAnswer() {
return super.getSecurityQuestionAnswer();
}
@Override
@Column(name = "token")
@AccessType(AccessType.Type.PROPERTY)
public String getToken() {
return super.getToken();
}
@Override
@ElementCollection
@OneToMany(cascade = CascadeType.ALL)
@AccessType(AccessType.Type.PROPERTY)
public Set<Playlist> getPlaylists() {
return super.getPlaylists();
}
@Override
@ElementCollection
@OneToMany(cascade = CascadeType.ALL)
@AccessType(AccessType.Type.PROPERTY)
public List<UserProfile> getUserProfiles() {
return super.getUserProfiles();
}
}
更新:
我发现Spring只抱怨userName
字段。当我将其添加到派生类时,应用程序不会崩溃。由于某种原因,它仅与其他字段一起使用。现在我实际上比以前更加困惑。
答案 0 :(得分:0)
您可能需要在超级类UserImpl
上使用@MappedSuperclass批注
请参阅文档https://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html