jpa

Skip child to fetching of parent - JPA

流过昼夜 提交于 2021-02-07 19:00:51
问题 I am facing an issue where the data is getting fechted recursively. I wanted to avoid the child to fetch the parent data. Which is causing a recursive issue. I have mentioned the code below Pojo Structure class Parent { .. @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY) private List<Child> childs; .. } class Child { .. @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "parentId") private Parent parent; .. } Fetching the data like this ` em = EMF.get().createEntityManager(); Query

hibernate @Entity on inner class only (top level class is not an @Entity)

夙愿已清 提交于 2021-02-07 18:47:54
问题 I would like to persist inner class into database. But it dosnt work. Is there possibilty to do that? Or should i put that inner class into new plain file? Now I am getting an error [IllegalArgumentException: Unknown entity: models.foo$bar] My class file: package models; public class foo { @Required public String report; @Required public String reportType; @Entity public static class bar{ @Required public int year; @Required public int month; public void toDataBase() { JPA.em().persist(this);

hibernate @Entity on inner class only (top level class is not an @Entity)

不打扰是莪最后的温柔 提交于 2021-02-07 18:46:56
问题 I would like to persist inner class into database. But it dosnt work. Is there possibilty to do that? Or should i put that inner class into new plain file? Now I am getting an error [IllegalArgumentException: Unknown entity: models.foo$bar] My class file: package models; public class foo { @Required public String report; @Required public String reportType; @Entity public static class bar{ @Required public int year; @Required public int month; public void toDataBase() { JPA.em().persist(this);

JPA Query Select many-to-one Query with a count

点点圈 提交于 2021-02-07 18:31:25
问题 So I'm trying to write a JPA query based on the classes below (dumbed down a bit) that will produce the following: So I have two objects: Thing and Person. A Person can hold a reference to a single Thing. Here are simplified version of the Classes: public class Thing { @Id public Long id; public String name; public String description; } public class Person { @Id public Long id; public String firstname; public String lastname; @ManyToOne public Thing thing; } I'm trying to write a JPA query

Can we build Spring Data JPA specification out of a composite key attribute

纵饮孤独 提交于 2021-02-07 18:12:29
问题 I am using Spring Data JPA specifications for generic queries to my entities. So far it has worked well. Now the problem happened when I try to use these on composite keys used via embeddedId annotation. So here I need to use a nested property to query which say if the object is Foo and I am trying to write a criteria over id. id1 for example. @Entity @Data public class Foo implements Serializable { private static final long serialVersionUID = 1L; /** The key. */ @EmbeddedId private FooKey id

EclipseLink / JPA: How to programmatically get the number of SQL queries that have been performed

▼魔方 西西 提交于 2021-02-07 13:34:48
问题 I'm using JPA, by way of EclipseLink. In my unit tests, I'd like to test how many SQL queries were performed during an operation. That way, if a later modification causes the query count to explode (if lazy loading is triggered, for instance), the unit test will flag it as potentially needing optimization. I'm striking out in finding the correct API to do this. A pure-JPA solution would be ideal, but I'm fine with using EclipseLink-specific APIs in my unit tests. I looked at the EclipseLink

JPA Query over a join table

◇◆丶佛笑我妖孽 提交于 2021-02-07 12:58:40
问题 I have 3 tables like: A AB B ------------- ------------ --------------- a1 a1,b1 b1 AB is a transition table between A and B With this, my classes have no composition within these two classes to each other. But I want to know that , with a JPQL Query, if any records exist for my element from A table in AB table. Just number or a boolean value is what I need. Because AB is a transition table, there is no model object for it and I want to know if I can do this with a @Query in my Repository

Hibernate and JPA - Error Mapping Embedded class exposed through an interface

一笑奈何 提交于 2021-02-07 12:57:15
问题 We have a set of interfaces, used as an API, and referenced from other modules. A set of concrete implementations of those interfaces, private to the "main" app module. These classes carry a number of annotations (JPA as well as XStream for XML serialization). I've run into a problem. We have a user class which had a number of fields within it related to location. We'd like to roll those up into an Address class. We want the data (for now) to remain in the same table. The approach is an

Continue with transaction after exception - JPA

ぃ、小莉子 提交于 2021-02-07 12:40:39
问题 I am using JPA with Spring. I am trying to do batch import. If there is problem with batch import then I would like to insert individually, and if this fails also then I would like to save to duplicates table. I wrote a logic for this but I get this error everytime: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly Mine setting for JPA are like this: @Bean(name = "dataSource", destroyMethod = "") public DataSource

JPA: TypedQuery sometimes returns null instead of NoResultException

房东的猫 提交于 2021-02-07 12:35:27
问题 Usually I work with NoResultException to return an "empty" object, e.g. an empty error list or new BigInteger("0"), if I get no results from a TypedQuery. Now it turned out that this sometimes doesn't work. Suddenly getSingleResult() returns null instead of causing a NoResultException, and I don't understand why. Look this example: public BigInteger pointsSumByAccountId(long accountId) { try { TypedQuery<BigInteger> pointsQuery = entityManager.createNamedQuery(Points.SumByAccountId,