jpa

Generic enum JPA AttributeConverter implementation

╄→гoц情女王★ 提交于 2021-02-18 07:39:28
问题 Problem I am trying to solve I am trying to implement enum mapping for Hibernate. So far I have researched available options, and both the @Enumerated(EnumType.ORDINAL) and @Enumerated(EnumType.STRING) seemed inadequate for my needs. The @Enumerated(EnumType.ORDINAL) seems to be very error-prone, as a mere reordering of enum constants can mess the mapping up, and the @Enumerated(EnumType.STRING) does not suffice too, as the database I work with is already full of values to be mapped, and

JPA Criteria API. Query with sql function call which takes parameters

心不动则不痛 提交于 2021-02-18 06:53:26
问题 I'm trying to construct this query using Criteria typesafe API: select * from xxx_table xxx where CALC_DISTANCE(xxx.latitude, xxx.longitude, :lat, :lng) < :dist CALC_DISTANCE definded PL/SQL function: FUNCTION calc_distance( pLat1 NUMBER, pLon1 NUMBER, pLat2 NUMBER, pLon2 NUMBER) RETURN NUMBER CriteriaBuilder builder = JpaHandle.get().getCriteriaBuilder(); CriteriaQuery<XXX> criteria = builder.createQuery(XXX.class); Root<XXX> xxxRoot = criteria.from(XXX.class); ParameterExpression<Double>

Merge an entity, change its id, merge again, cause “mapped to a primary key column in the database. Updates are not allowed” error

百般思念 提交于 2021-02-18 05:20:44
问题 I have a JPA program where EclipseLink is the Persistence provider. When I merge an user entity, change its ID, and try to merge the same user instance again, an error is thrown. I rewrite my code to illustrate my problem in the simplest way. User user = userManager.find(1); userManager.merge(user); System.out.println("User is managed? "+userManager.contains(user); user.setId(2); userManager.merge(user); The above code is not in a transaction context. userManager is a stateless session bean

Date comparison using the JPA criteria API

笑着哭i 提交于 2021-02-18 02:59:45
问题 I got a range date picker with two dates: start and end , where both can be empty. I would like to filter a table, where the entity has exact one date: date . So, here are some examples. I'd like to match. Imaging the date to match is the current date (17/07/2016). null - 17/07/2016 -> match 17/07/2016 - null -> match 17/07/2016 - 17/07/2016 -> match null - null -> match all Those are the edge cases in my opinion and the reason why I am struggling so much. Actually my code looks like:

JPA: Having lists on both ends without infinite loop

∥☆過路亽.° 提交于 2021-02-17 05:28:08
问题 I am trying to link two entities via Set properties like: Entity A: Set<Group> groups Entity B: Set<Filter> filters However, I keep getting errors like infinite recursions. What is the best way to do this with JPA? 回答1: It goes into infinite recursion because both your entities call each other and it will never stop. Try adding @JsonManagedReference(value = "group-filter") Set<Group> groups and @JsonBackReference(value = "user-card") Set<Filter> filters above both the sets in your entities.

JPA, Reuse of deleted ID

江枫思渺然 提交于 2021-02-17 05:22:09
问题 I use an Entity with generated ID (so Entity has a field, that marked by @Id and @GeneratedValue ). I'm going to use generated id of the entities in the business logic and I have a question: If I delete an entity, its ID will be re-used or not? I.e. new generated ID can be only bigger that all generated before? 回答1: You don't need to reuse IDs, and shouldn't need to waste time worrying about this. (This is a beginner's database question.) In general, reusing IDs would technically be possible

JPA Entity class giving error with 2 @GeneratedValue fields

霸气de小男生 提交于 2021-02-17 05:11:12
问题 I have two columns which will use @GeneratedValues, but when I am putting them like this it is giving error; " Exception Description: Class [class Testing] has two @GeneratedValues: for fields [Testing.SEQ_NO] and [Testing.ID]. Only one is allowed. " @Table(name = "TABLE1") @Entity public class Testing{ @GeneratedValue(strategy=GenerationType.AUTO) @Id private Long id; @Column(name = "LINKAGE_ID") private int linkageId; @Column(name = "TRANSFER_ID") private int transferId; @Column(name =

How to map sql native query result into DTO in spring jpa repository?

穿精又带淫゛_ 提交于 2021-02-17 04:46:33
问题 Hi what I am trying to achieve is to get SQL native query result map into my DTO in java spring jpa repository, how do I do this properly? I try several code, but it does not work, here is what I tried: First try : @Repository public interface StockRepository extends RevisionRepository<Stock, Long, Integer>, JpaRepository<Stock, Long> { @Query(value = "SELECT stock_akhir.product_id AS productId, stock_akhir.product_code AS productCode, SUM(stock_akhir.qty) as stockAkhir " + "FROM book_stock

Getting all children and subchildren from parent JPA

感情迁移 提交于 2021-02-16 20:55:09
问题 I have a table TABLE that can have parents of the same type. In Java the child can reach the parent but the parent doesn't have a list of children. In MySQL I was able to create the following query that gives me the children and subchildren of the parent, but I'm unable to translate this into JPA Java. How can I translate this query: SELECT * FROM TABLE AS T1 INNER JOIN (SELECT id FROM TABLE WHERE TABLE.parentId = 966) AS T2 ON T2.id = T1.parentId OR T1.parentId = 966 GROUP BY T1.id Into java

Stackoverflow error when saving an Object's toString value - Java/Hibernate/Spring

ぐ巨炮叔叔 提交于 2021-02-16 20:07:37
问题 I have the following sample entities: Institution @Data @Entity @NoArgsConstructor @EntityListeners(InstitutionAuditListener.class) public class Institution extends Auditable<String> { @OneToMany(mappedBy = "institution", cascade = CascadeType.ALL) @JsonManagedReference private List<RegisteredProgram> registeredPrograms; private String name; } RegisteredProgram @Data @NoArgsConstructor @Entity @EntityListeners(RegisteredProgramAuditListener.class) public class RegisteredProgram extends