2012-03-12 67 views
0

我有使用JPA類libary,問題是,當它有時運行我得到這個錯誤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

您需要向我們展示一些代碼,例如AppProfile類。它是否注有@Entity? – Erik 2012-03-12 19:09:31

回答

0

您包括AppProfile在你的persistence.xml,但你不代碼包括AppUsers。

+0

我有一個錯字,現在被糾正,但我有同樣的問題。 – alvariux 2012-03-12 21:33:43

相關問題