Tag: jpa

JPA CriteriaBuilder – 按一对多关系中的关联实体数量排序

我有两个实体Customer和Order,一对多关系。 对于每个客户,我需要计算相关订单的数量,并按此数字对结果进行排序。 在本机postgres查询中,它看起来像这样: select cust.id, count(order.id) from customers cust left outer join orders order on cust.id = order.customer_id where …. conditions … group by cust.id order by count desc; 但我必须使用CriteriaBuilder执行此操作,因为此查询是使用CriteriaBuilder添加其他条件的更大代码段的一部分。 在Hibernate中我可能会使用Projections,但我在JPA中找不到类似的东西。 任何使用CriteraBuilder编写查询的帮助都将非常感激。 先谢谢你。

JPQL:查询多个列时,哪种对象包含结果列表?

我正在尝试做一些在PHP&Co中很容易的事情:SELECT COUNT(x)as numItems,AVG(y)as average,… FROM Z 在PHP中我会得到一个像[{numItems:0,average:0}]这样的简单数组,我可以这样使用: echo “Number of Items: ” . $result[0][‘numItems’]; 通常在JPQL中,您只查询单个对象或单个列并获取Lists类型,例如List或List 。 但是,在查询多个列时,您会得到什么?

使用JSF,JPA和DAO。 没有spring?

到目前为止,我仍然使用JSF和JPA而没有DAO。 现在我想使用DAO。 但是如何在DAO-Classes中初始化EntityManager? public class AdresseHome { @PersistenceContext private EntityManager entityManager; public void persist(Adresse transientInstance) { log.debug(“persisting Adresse instance”); try { entityManager.persist(transientInstance); log.debug(“persist successful”); } catch (RuntimeException re) { log.error(“persist failed”, re); throw re; } } } 我有没有使用Spring,还是有一个没有Spring的解决方案? 谢谢。

JPA 2.1 Hibernate中的NamedSubgraph忽略嵌套子图

我正在使用Hibernate 4.3.8.FINAL并且具有以下模型,其中Department有许多Employees,Employee可以是Manager。 经理有一套Foo,可以是Foo或Bar。 员工实体: @Entity @Table(name = “employee”, schema = “payroll”) @Inheritance(strategy = InheritanceType.JOINED) public class Employee { @Id private Long id; @Basic(optional = false) @Column(name = “name”) private String name; @JoinColumn(name = “department_id”, referencedColumnName = “id”) @ManyToOne(optional = false, fetch = FetchType.LAZY) private Department department; } 经理实体: @Entity @Table(name = “manager”, schema = “payroll”) […]

Hibernate一级缓存 – 是否同步?

我知道Session是Hibernate使用的第一级缓存,一旦我们从session检索实体,后续对具有相同标识符的同一实体的 get调用将从session而不是DB中获取,直到session为止。 打开 。 话虽如此,我对hibernate如何将第一级缓存与DB同步有疑问? 请考虑以下情形 //Lets say I have created the session Session s1 = sessionFactory.getSession(); User u1 = s1.get(User.class, 1); //Getting User with ID=1 //s1 is not yet closed //Lets say I create some other session Session s2 = sessionFactory.getSession(); User u2 = s2.get(User.class, 1); //Getting User with ID=1 u2.setName(“Abc”); // Changed a […]

使用Hibernate和JPA映射Map

我尝试以下映射 @ElementCollection private Map doubleValues; 但是当我从这里生成我的模式时(使用mvn hibernate3:hbm2ddl ),我得到以下错误: org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: ImagerAnalysisResult, for columns: [org.hibernate.mapping.Column(doubleValues)] 我已经尝试添加列和类型信息,但我一直得到相同的错误。 另外,我不认为应该给出信息,因为hibernate应该使用generics声明(根据如何映射Map )。 我正在使用hibernate版本3.6.4.Final,我尝试过其他版本 有什么建议么? 谢谢。 编辑:似乎maven hibernate插件已经两年了,依赖于较旧的hibernate版本…为什么这个插件没有被维护? 编辑:命名字段“doubleValues”以确保没有保留名称问题。

没有交易的JPA

我是JPA的新手。 我正在开发一个使用JPA(Hibernate实现)和Spring的应用程序。 我在我的persistence.xml中声明了一个持久性单元,并在我的Spring配置文件中配置了关于EntityManagerFactory的配置。 像这样的东西: create-drop org.hibernate.dialect.MySQL5Dialect 然后我有一些DAO,我用@PersistenceContext注释注入entityManager: public MyDaoImpl implements MyDao{ private EntityManager entityManager; @PersistenceContext private void setEntityManager(EntityManager em){ this.entityManager = em; } } 最后,我有一些注入DAO的服务(通过@Autowired Spring的注释): public MyServiceImpl implements MyService{ @Autowired private MyDao myDao; public List readOperation(){ // return myDAo.searchAll(); } } 作为一个只读操作,我认为它不需要@Transactional注释,但没有它,有一个例外: java.lang.IllegalStateException: No transactional EntityManager available at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:223) at $Proxy121.unwrap(Unknown Source) 我读过其他一些post: java.lang.IllegalStateException:没有可用的事务性EntityManager […]

Hibernate @Proxy(lazy = false)注释有什么作用?

在尝试序列化作为JPA实体的ESRBRating对象时,我遇到了两个不同的堆栈跟踪(见下文)。 我正在使用Spring Data JPA。 控制器调用服务,称为存储库的服务。 我能够通过在我的ESRBRating对象上添加@Proxy(lazy = false)来解决问题。 我的主要问题是@Proxy(lazy = false)实际上做了什么? 添加时为什么会起作用? 这是一个很好的解决方案,还是会产生诸如性能/内存问题等副作用? 作为参考,这是我的ESRBRating课程。 @Entity @Table(name = “esrb_rating”, schema = “igdb”) @JsonIgnoreProperties(ignoreUnknown = true) @Proxy(lazy = false) public class ESRBRating implements Serializable { private static final long serialVersionUID = 1L; @Id @NotNull @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = “id”, nullable = false) private Long id; @NotNull […]

Maven不生成“persistence.xml”文件

我在maven项目网页上阅读了这篇文章 ,其中列出了不同的目录布局(例如:src / main / resources,用于Application / Library资源)。 问题是当我运行以下命令( 在这里找到 )时: mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 未创建src / main / resources / META-INF目录。 这对我很重要,因为我想要访问该目录中的“persistence.xml”。 我应该在mvn命令中添加一个选项吗? 如何自动生成包含“META-INF / persistence.xml”文件的“src / main / resources”? 谢谢,问候

检索JPA实体列表和元数据

我想知道是否有办法在JPA中获取特定持久单元的所有实体类及其元数据。 通过元数据,我不仅指字段,还指它们的列名,长度,精度,数据类型,以及表名和我可以获得的任何内容。 我尝试使用元模型,但我认为仅仅针对JPQL查询。 我需要能够向用户显示某些PU的所有活动实体,并且我不想在某些数组或数据库中对它们进行硬编码,我希望API告诉我它有什么实体。 而且,如果可能的话,获取每个实体的托管实例。 我想我可以尝试使用reflection来获取带有@Entity注释的所有类,但它不会很漂亮,并且更难以知道哪个类属于特定的PU,所以如果api已经暴露了这个信息,那么它将是大。 我更喜欢符合JPA的解决方案,但如果不可能,Hibernate或EclipseLink特定的答案就可以做到。 谢谢!