gpt4 book ai didi

java - 错误: Not an entity.但是@Entity和persistence.xml正确

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

我有两个项目,一个是另一个的 Maven 依赖项。两者都有独立的 PostgreSQL 数据库。如果我运行应用程序,当我尝试在依赖项目中使用依赖项目的实体时,就会抛出“不是实体”错误。

我已经尝试将事务类型设置为 JTA 或 RESSOURCE-LOCAL 或根本不设置。我还使用 Eclipse JPA 工具从废料中再次生成了实体。

用户.java:

/**
* The persistent class for the "Users" database table.
*
*/
@Entity
@Table(name = "\"Users\"")
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")
public class User implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Integer id;

private String mail;

private String password;

// bi-directional many-to-many association to Role
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "\"User_Role\"", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = {
@JoinColumn(name = "role_id") })
private Set<Role> roles;

// bi-directional many-to-many association to Permission
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "\"User_Permission\"", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = {
@JoinColumn(name = "permission_id") })
private Set<Permission> permissions;

public User() {
}

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getMail() {
return this.mail;
}

public void setMail(String mail) {
this.mail = mail;
}

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

public Set<Permission> getPermissions() {
return this.permissions;
}

public void setPermissions(Set<Permission> permissions) {
this.permissions = permissions;
}

public Set<Role> getRoles() {
return this.roles;
}

public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}

持久性.xml:

    <persistence-unit name="Authentication" transaction-type="JTA">
<class>com.auticon.learning.authentication.entities.User</class>
<class>com.auticon.learning.authentication.entities.Permission</class>
<class>com.auticon.learning.authentication.entities.Role</class>

<properties>
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"></property>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/Credentials"></property>
<property name="javax.persistence.jdbc.user" value="postgres"></property>
<property name="javax.persistence.jdbc.password" value="******"></property>
</properties>
</persistence-unit>

抛出错误的类:

    public static User findUser(String username) {
CriteriaBuilder builder = SessionConfig.getSession().getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = builder.createQuery(User.class);

//Error is thrown when I try to get the Root Parameter of the Entity
Root<User> root = criteriaQuery.from(User.class);

criteriaQuery.where(builder.equal(root.get(User_.MAIL), username));
Query<User> query = SessionConfig.getSession().createQuery(criteriaQuery);

try {
return query.getSingleResult();
} catch (NoResultException e) {
return null;
}
}

在其他项目中调用类,这取决于上述项目(是的,大部分来自 Primefaces Showcase :P ):

    public void login() {
FacesMessage message = null;
boolean loggedIn = false;

//Login.checkPW() first action is calling findUser()
if (Login.checkPW(username, password)) {
loggedIn = true;
message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", username);
} else {
loggedIn = false;
message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error", "Invalid credentials");
}

FacesContext.getCurrentInstance().addMessage(null, message);
PrimeFaces.current().ajax().addCallbackParam("loggedIn", loggedIn);
}

Screen of the PostgreSQL DB(我不能直接在帖子中发布图片)

JPA 意识到我已经正确注释了实体,将其列在 persistence.xml 中,并且表确实存在于数据库中。

完整的堆栈跟踪:

引起:java.lang.IllegalArgumentException:不是实体:类com.auticon.learning.authentication.entities.User 在org.hibernate.metamodel.internal.MetamodelImpl.entity(MetamodelImpl.java:536) 在 org.hibernate.query.criteria.internal.QueryStructure.from(QueryStructure.java:126) 在 org.hibernate.query.criteria.internal.CriteriaQueryImpl.from(CriteriaQueryImpl.java:153) 在 com.auticon.learning.authentication.client.DBOperations.findUser(DBOperations.java:22) 在 com.auticon.learning.authentication.api.Login.checkPW(Login.java:9) 在de.auticon.views.UserLoginView.login(UserLoginView.java:43) 在 sun.reflect.NativeMethodAccessorImpl.invoke0( native 方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181) 在 com.sun.el.parser.AstValue.invoke(AstValue.java:289) 在 com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304) 在 org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) 在 org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) 在 org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) 在 org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) 在 com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:65) 在com.sun.faces.application.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:66) ... 58 更多

最佳答案

亲爱的,请检查您的配置文件(hibernate.xml 或 persistence.xml)中实体 bean 的条目。实际上,我们的持久化上下文是基于我们的配置文件构建的,因此如果您的实体文件没有在其中注册,框架不会将其视为实体 bean/类。

关于java - 错误: Not an entity.但是@Entity和persistence.xml正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56887215/

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