hibernate

JPA filtering entities from a mapped association

安稳与你 提交于 2021-01-29 21:22:03
问题 My friendships sql table has column friend_1, friend_2, status . I have the following mappings in my (snippet below) User class. Currently user.getInitiatedFriendships returns list of Friendships where value of column friend_1 is equal to ID of the user I'm calling this getter on. user.getInvitedToFriendships() returns Friendships where column friend_2 is equal to ID of this user. I want to know if it is possible to add something to this line @OneToMany(mappedBy = "invitingUser" +>some check

rewrite SQL query to HQL

被刻印的时光 ゝ 提交于 2021-01-29 20:19:32
问题 I've SQL query: SELECT tag.id, tag.description, tag.`name`, COUNT(question_has_tag.question_id) FROM question_has_tag INNER JOIN question ON question.id = question_has_tag.question_id INNER JOIN tag ON question_has_tag.tag_id = tag.id GROUP BY tag.id All works good. But I need to rewrite it to HQL, some one have any idea? I tried this, but it doesn't work: list = entityManager.createQuery("SELECT " + "t.id, " + "t.description, " + "t.name, " + "COUNT(q.id) " + "FROM Tag t INNER JOIN Question

JPA filtering entities from a mapped association

 ̄綄美尐妖づ 提交于 2021-01-29 19:06:06
问题 My friendships sql table has column friend_1, friend_2, status . I have the following mappings in my (snippet below) User class. Currently user.getInitiatedFriendships returns list of Friendships where value of column friend_1 is equal to ID of the user I'm calling this getter on. user.getInvitedToFriendships() returns Friendships where column friend_2 is equal to ID of this user. I want to know if it is possible to add something to this line @OneToMany(mappedBy = "invitingUser" +>some check

rewrite SQL query to HQL

烂漫一生 提交于 2021-01-29 16:51:48
问题 I've SQL query: SELECT tag.id, tag.description, tag.`name`, COUNT(question_has_tag.question_id) FROM question_has_tag INNER JOIN question ON question.id = question_has_tag.question_id INNER JOIN tag ON question_has_tag.tag_id = tag.id GROUP BY tag.id All works good. But I need to rewrite it to HQL, some one have any idea? I tried this, but it doesn't work: list = entityManager.createQuery("SELECT " + "t.id, " + "t.description, " + "t.name, " + "COUNT(q.id) " + "FROM Tag t INNER JOIN Question

Spring Boot JPA CrudRepository - entities are not attached to persistencecontext

ε祈祈猫儿з 提交于 2021-01-29 16:47:01
问题 I'm currently implementing my first spring boot application after years of JEE. I am wondering about JPA / Hibernate behaviour. In JEE, once persisted or "find" an @Entity, all changes to this Entity are populated automatically by the JPA implementation to the database without any need to call persist(), flush() etc. Now with a CrudRepository changes are only stored to the database if I explicitely call save(). My CrudRepository: public interface UserAccountRepository extends CrudRepository

Silly Question about a validation case in Java and temporary variables

余生长醉 提交于 2021-01-29 15:54:16
问题 How does one set a temporary variable of date before it is updated? I would like to compare my initial date before it is updated. Update: More code * @hibernate.class table="t3sstations" * @hibernate.join name="rgnjoin" table="stnregions" optional="true" * @hibernate.join-key column="idstn" */ public class Station extends BaseSiteChildCFKObject implements Convertible, Comparable<Station> { private static final long serialVersionUID = -2056063261398467275L; protected final Log log = LogFactory

Using the same predicates for two criteria queries

孤人 提交于 2021-01-29 14:52:39
问题 I want to run a pair of queries using the same array of Predicate : one to count the records, one to get a certain page of records. This seems like a pretty normal use case, to me, so there must be a good way to do it, but I have yet to find it. So this is the part that fetches entities: CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); EntityType<ENTITY> entityType = entityManager.getMetamodel().entity(FooEntity.class); CriteriaQuery<FooEntity> entityQuery =

Hibernate Transactions and Concurrency Using attachDirty (saveOrUpdate)

放肆的年华 提交于 2021-01-29 14:24:43
问题 I have the following case where attachDirty saveOrUpdate() always tries to persist the entities when the transactional annotated method is called in burst. Is there a way to avoid this primary key constraint violation exception? I thought that saveOrUpdate() would persist if the entity is not yet created and update it otherwise. I used this ..orUpdate() instead of persist() as a way to handle these bursts. There is no data corruption actually. Because the first saveOrUpdate() does save 1

how to use JPA Native query and TypedParameterValue to perform count

笑着哭i 提交于 2021-01-29 14:20:14
问题 I have an Entity class and I want to count the number of elements that satisfy some conditions in a PostgreSQL database. The entity contains many columns but I just need a response that returns the count of the selected columns as pending, approved, notSubmitted, submitted, approved, notapproved, queried... In order to avoid type mismatch from PostgreSQL driver, I am using the TypedParameterValue method. However I am getting a null value as a response instead of the actual value that should

SQL Error: 17068 while inserting records in hibernate

≯℡__Kan透↙ 提交于 2021-01-29 13:41:49
问题 When am trying to insert record hibernate throws SQL Error: 17068, SQLState: 99999 so which is " There are invalid arguments in the call " i checked every properties of my model class . I think it's because of dates but i couldn't have solution for insert records to database.Thanks in advance My Controller Class: @RestController @Slf4j public class MainController { @Autowired KullanicilarServis kullaniciServis; @PostMapping("/kayitol") public ResponseEntity<Kullanicilar> createUser(