gpt4 book ai didi

java - Spring 启动+JPA : "Unable to locate Attribute with the given name" in a derived class

转载 作者:行者123 更新时间:2023-12-02 04:02:32 30 4
gpt4 key购买 nike

我正在开发一个使用 Spring Boot + JPA 来保存一些实体的应用程序。然而,这些实体(当然还有 Spring 配置)是应用程序中唯一依赖于 Spring/JPA 的东西。这就是为什么我们想将应用程序分成两个组件。核心应用程序应该独立于 Spring 和任何与 JPA 相关的事物以及 Spring 应用程序。为此,我们在核心应用程序中创建了一个接口(interface)和实体的基本实现,并在 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 字段。当我将它添加到派生类时,应用程序不会崩溃。由于某种原因,它只适用于其他领域。现在我其实比以前更困惑了。

最佳答案

您可能需要在父类(super class)上使用 @MappedSuperclass 注释UserImpl

查看文档 https://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html

关于java - Spring 启动+JPA : "Unable to locate Attribute with the given name" in a derived class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56726249/

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