2012-03-12 17 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]. 

I再実行プロジェクトがうまく実行した場合。私はときどきうまくいって、ときどき実行しないということを意味します。

誰かがこれがどうして起こるかを伝えることができますか?

私はpersistence.xmlでクラスを追加した

com.mycompany.db.AppProfile

EDIT

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> 

これは、「あなたは、あなたのpersistence.xmlでAppProfileが含まれていますがドン時々

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

答えて

0

を失敗したコードですAppUsersを含めます。

+0

私はタイプミスがありましたが、修正されましたが、私は同じ問題を抱えています。 – alvariux