我有一个使用JPA类库的Java SE项目,问题是当它运行时有时会出现这个错误:
Exception in thread "main" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Error compiling the query [select profile from AppProfile profile where profile.user_id=?1]. Unknown entity type [AppProfile].
如果我重新运行该项目执行得很好。我的意思是有时运行正常,有时不运行
有人能说出为什么会这样吗?
我在persistence.xml中添加了这个类
com.mycompany.db.AppProfile
修改
AppUsers.class
@Entity
@Table(name="app_users")
@NamedQueries({
@NamedQuery(
name="getAllUsers",
query="SELECT usr FROM AppUsers usr order by usr.username asc"
),
@NamedQuery(name="findUserByUserName",
query="SELECT usr FROM AppUsers usr WHERE usr.username= ?1")
})
public class AppUsers implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="firstname",length=100,nullable=false)
private String firstname;
@Column(name="lastname",length=100,nullable=false)
private String lastname;
@Column(name="username",length=100,nullable=false)
private String username;
@Column(name="password",length=100,nullable=false)
private String password;
@Column(name="email",length=150,nullable=false)
private String email;
@Column(name="status",nullable=false)
private int status = 0;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
AppProfile.class
@Entity
@Table(name="app_profile")
@NamedQueries({
@NamedQuery(
name="getUserProfile",
query="SELECT profile FROM AppProfile profile where profile.user_id=?1"
)
})
public class AppProfile implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name="user_id",nullable=false)
private Long user_id;
@Column(name="address",length=255,nullable=false)
private String address;
@Column(name="state",length=100,nullable=false)
private String state;
@Column(name="city",length=100,nullable=false)
private String city;
@Column(name="phone",length=100,nullable=false)
private String phone;
@Column(name="mobile",length=100,nullable=false)
private String mobile;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "user_id", insertable=false,updatable=false)
private AppUsers userdata;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getUserId()
{
return this.user_id;
}
public AppUsers getUser()
{
return this.userdata;
}
}
persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="MyDBPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.mycompany.db.AppUsers</class>
<class>com.mycompany.db.AppProfile</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myapp"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value=""/>
</properties>
</persistence-unit>
</persistence>
这是有时失败的代码
public getProfile(Long user_id)
{
EntityManagerFactory emf=Persistence.createEntityManagerFactory("MyDBPU");
EntityManager em=emf.createEntityManager();
AppProfile result = null;
try{
EntityTransaction entr=em.getTransaction();
entr.begin();
String jpql="select profile from AppProfile profile where profile.user_id=?1";
Query query=em.createQuery(jpql);
query.setParameter(1, user_id);
result=(AppProfile) query.getSingleResult();
}
catch(NoResultException e){
result=null;
}
finally{
em.close();
}
return result;
}
由于
答案 0 :(得分:0)
您在persistence.xml中包含AppProfile,但不包括AppUsers。