我正在尝试使用JPA通过以下方式在eclipse微观配置文件中进行简单的get查询:
Class Player:
@Entity
@Table(name = "player")
public class Player {...
主类:
@ApplicationScoped
@Path("/hello2")
public class HelloWorld extends Application {
@Path("/players")
@GET
public List<Player> getPlayers() {
EntityManager entityManager = Persistence.createEntityManagerFactory("testMicroProfile").createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
List<Player> list = null;
transaction.begin();
TypedQuery<Player> query = entityManager.createQuery("SELECT p FROM Player p", Player.class);
list = query.getResultList();
transaction.commit();
return list;
}
}
这是文件persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="testMicroProfile" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/microprofiledb"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="psw"/>
</properties>
</persistence-unit>
</persistence>
数据库中表的名称为player
,但是我在关于stackoverflow的另一个答案中读到了我必须在查询中使用类的名称,因此我在“播放器”,但仍然无法正常工作。缺少什么?
答案 0 :(得分:0)
尝试将您的实体类添加到 persistence.xml :
<persistence-unit name="testMicroProfile">
<class>my.test.Player</class>