下午好,我目前正在使用JPA连接数据库,但是在尝试连接时出现了一些错误,我实在不明白。
我的代码的主要思想是使用实体管理器连接到本地数据库,并从数据库中选择所有信息,然后将图书价格(千卡)降低10%,并将所有更改提交给数据库。
通过方式,我知道书名(nazov)不适合用作主键,但是在这种情况下,我只是在测试并尝试JPA可以做什么。
主程序:
package cviko2;
import javax.persistence.*;
import java.util.*;
/**
*
* @author vsa
*/
public class Cviko2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
EntityManagerFactory emf = Persistence.createEntityManagerFactory("p1app2PU");
EntityManager em = emf.createEntityManager();
Query q = em.createNativeQuery("select * from KNIHA", Kniha.class);
List<Kniha> r = (List<Kniha>) q.getResultList();
em.getTransaction().begin();
for(Kniha k: r){
k.setCena((float) (k.getCena() * 0.9));
}
em.getTransaction().commit();
}
}
尼哈级:
package cviko2;
import java.io.*;
import javax.persistence.*;
/**
*
* @author vsa
*/
@Entity
public class Kniha implements Serializable {
@Id
private String nazov;
private float cena;
public Kniha(String nazov, float cena) {
this.nazov = nazov;
this.cena = cena;
}
public float getCena() {
return cena;
}
public void setCena(float cena) {
this.cena = cena;
}
public String getNazov() {
return nazov;
}
public void setNazov(String nazov) {
this.nazov = nazov;
}
}
最后是错误-.-
[EL Info]: 2019-03-03 17:18:26.674--ServerSession(22429093)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[EL Info]: connection: 2019-03-03 17:18:26.897--ServerSession(22429093)--file:/home/vsa/NetBeansProjects/Cviko2/build/classes/_p1app2PU login successful
[EL Warning]: metamodel: 2019-03-03 17:18:26.92--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
[EL Warning]: 2019-03-03 17:18:27.005--UnitOfWork(393040818)--Exception [EclipseLink-6007] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: Missing descriptor for [class cviko2.Kniha].
Query: ReadAllQuery(referenceClass=Kniha sql="select * from KNIHA")
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-6007] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: Missing descriptor for [class cviko2.Kniha].
Query: ReadAllQuery(referenceClass=Kniha sql="select * from KNIHA")
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:480)
at cviko2.Cviko2.main(Cviko2.java:26)
Caused by: Exception [EclipseLink-6007] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: Missing descriptor for [class cviko2.Kniha].
Query: ReadAllQuery(referenceClass=Kniha sql="select * from KNIHA")
at org.eclipse.persistence.exceptions.QueryException.descriptorIsMissing(QueryException.java:478)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkDescriptor(ObjectLevelReadQuery.java:821)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:819)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1127)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:403)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1215)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1786)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1751)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:469)
... 1 more
/home/vsa/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
答案 0 :(得分:0)
实体类必须具有公共或受保护的无参数构造函数。 该类可能还有其他构造函数。
如果使用参数创建构造函数,则没有默认的无参数构造函数。向实体添加无参数构造函数。