jpa

Spring boot JPA many to many with extra column insert and update issue

吃可爱长大的小学妹 提交于 2021-02-11 12:59:24
问题 Here is my initial question. Spring Data JPA Many to Many with extra column User and Roles Now I have the right tables created, but can't make it work for the update. Here is the code: User.java @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; @OneToMany(mappedBy="user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) private List<UserRole> roles; // getters and setters

H2 schema still generated when spring.jpa.generate-ddl=false

大城市里の小女人 提交于 2021-02-11 12:33:32
问题 I am using Spring Boot 2.1.3 with an H2 in memory database for testing. When I run my tests, the schema gets generated even when I specify the following property. spring.jpa.generate-ddl=false It seems that because Spring Boot defaults the following property when using H2 spring.jpa.hibernate.ddl-auto=create-drop That this takes precedence over spring.jpa.generate-ddl=false Is this a bug? 来源: https://stackoverflow.com/questions/55575505/h2-schema-still-generated-when-spring-jpa-generate-ddl

Skip NonUniqueDiscoveredSqlAliasException when two table has same column names

蓝咒 提交于 2021-02-11 12:29:29
问题 My existing application is using HibernateDAOSupport and hibernateTemplate with version 3.6.10. When I have a native query like below then it works fine with current system. select a.name, b.name from a inner join b on a.bid=b.id Now I am upgrading my application to JPA so it throws me error : NonUniqueDiscoveredSqlAliasException I can fix this error by changing query to below one: select a.name as aname, b.name from a inner join b on a.bid=b.id But the problem is my application is huge and

Cannot catch DataIntegrityViolationException

青春壹個敷衍的年華 提交于 2021-02-11 06:49:28
问题 I am using Spring Boot 2 with spring-boot-starter-data-jpa with an underlying MariaDB. I have table with a unique key "username". I want to catch DataIntegrityViolationException if this constraint is violated, but it seems like Spring is logging DataIntegrityViolationException and does not rethrow the after logging(my best guess). MySQLIntegrityConstraintViolationException is thrown instead. I would like to catch DataIntegrityViolationException in UserService.createUser(..) . Here are a

Cannot catch DataIntegrityViolationException

邮差的信 提交于 2021-02-11 06:48:12
问题 I am using Spring Boot 2 with spring-boot-starter-data-jpa with an underlying MariaDB. I have table with a unique key "username". I want to catch DataIntegrityViolationException if this constraint is violated, but it seems like Spring is logging DataIntegrityViolationException and does not rethrow the after logging(my best guess). MySQLIntegrityConstraintViolationException is thrown instead. I would like to catch DataIntegrityViolationException in UserService.createUser(..) . Here are a

Spring boot JPA no returning existing result using findById

心不动则不痛 提交于 2021-02-11 02:45:31
问题 I have created a pretty small and simple Spring Boot app using the Oracle database and some JPA queries. This is the code snippet which is not returning data, which is actually exists in database. letterRecipientNonOas = letterRecipientNonOasRepository .findById(Long.valueOf(letterRecipientDTO.getNonOas().getId())) .orElseThrow(() -> new EntityNotFoundException(LetterRecipientNonOas.class, Constant.MESSAGE_ENTITY_NOT_FOUND)); here findById is returning empty result set. this is my repository

JPA (Hibernate) + Spring: Dealing with unique constraint violations

大憨熊 提交于 2021-02-10 20:26:21
问题 I have a entity A with a unique field and that field basically defines the entity, meaning it it is equal then then entity is also exactly the same. A second point is that it is lets say in no way exceptional if that constraint is violated. Meaning it is fully expected that users will try to input duplicates. In case of duplicate, the application should silently chose the already existing entity. My question is now what I should do especially when saving entities containing a list of As. just

How to mock EntityManager

谁都会走 提交于 2021-02-10 20:15:51
问题 I first posted a question on here about a Unit test, EntityManager and NullPointerException. And then somebody advised me to look up mocking, so I did. However, I am still facing the same issues. I like to know if I am missing something? Is there a setup I forgot to add? Do I require to indicate the location of the persistence XML file? I would appreciate any help with that. I looked at other similar questions. However, I didn’t have much luck. 1./ How to mock object in EntityManager? 2./ How

How to know if a class is an @Entity (javax.persistence.Entity)?

江枫思渺然 提交于 2021-02-10 18:17:22
问题 How can I know if a class is annotated with javax.persistence.Entity ? Person (Entity) @Entity @Table(name = "t_person") public class Person { ... } PersonManager @Stateless public class PersonManager { @PersistenceContext protected EntityManager em; public Person findById(int id) { Person person = this.em.find(Person.class, id); return person; } I try to do it with instance of as the following @Inject PersonManager manager; Object o = manager.findById(1); o instanceof Entity // false however

Spring Boot JPA Insert and Update

◇◆丶佛笑我妖孽 提交于 2021-02-10 12:47:46
问题 Alright, I've looked around to find this answer for about an hour and I can't see it posted. So I bought the Spring Framework Master Class from in28minutes on Udemy. We have started implementing JPA. However, the Spring Boot versions are different( he is using 2.0.3, I am using 2.4.0). Now I know that's the issue. So the task is to simply connect to a h2 database, and interact with the data. Here is the current code setup I am using: JpaDemoApplication.java package com.in28minutes.database