我正在尝试使用 JPA 在 eclipse 微配置文件中执行简单的 get 查询,这样:
班级球员:
@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 上的另一个答案中读到我必须在查询中使用类的名称,所以我在“播放器”中更改了它,但不起作用反正。少了什么东西?