Hibernate有一些方法,以这种或那种方式,获取对象并将其放入数据库.它们之间有什么区别,什么时候使用哪个,为什么不存在一个知道何时使用什么的智能方法?
到目前为止我发现的方法是:
save()update()saveOrUpdate()saveOrUpdateCopy()merge()persist()我正在尝试使用HQL使用JOIN FETCH获取我的实体以及子实体,如果我想要所有结果,这是正常工作但如果我想要一个页面则不是这样
我的实体是
@Entity
@Data
public class VisitEntity {
@Id
@Audited
private long id;
.
.
.
@OneToMany(cascade = CascadeType.ALL,)
private List<VisitCommentEntity> comments;
}
Run Code Online (Sandbox Code Playgroud)
因为我有数百万次访问,我需要使用Pageable,我想在单个数据库查询中获取注释,如:
@Query("SELECT v FROM VisitEntity v LEFT JOIN FETCH v.comments WHERE v.venue.id = :venueId and ..." )
public Page<VisitEntity> getVenueVisits(@Param("venueId") long venueId,...,
Pageable pageable);
Run Code Online (Sandbox Code Playgroud)
该HQL调用抛出以下异常:
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=com.ro.lib.visit.entity.VisitEntity.comments,tableName=visitdb.visit_comment,tableAlias=comments1_,origin=visitdb.visit visitentit0_,columns={visitentit0_.visit_id ,className=com.ro.lib.visit.entity.VisitCommentEntity}}] …Run Code Online (Sandbox Code Playgroud) 我有一份基于计时器的工作
@Component
public class Worker {
@Scheduled(fixedDelay = 100)
public void processEnvironmentActions() {
Job job = pickJob();
}
public Job pickJob() {
Job job = jobRepository.findFirstByStatus(Status.NOT_PROCESSED);
job.setStatus(Status.PROCESSING);
jobRepository.save(job);
return job;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在大多数情况下,这应该给我正确的结果。但是如果有两个微服务实例同时执行这段代码会怎样呢?
我如何确保即使有多个服务实例,存储库也应该始终只为一个实例而不是其他实例提供一项工作。
编辑:我认为人们越来越困惑/专注于@Transactional所以删除它。问题保持不变。
我是探索 Spring Boot 和 Hibernate 的新手,并面临着一个我认为并不新鲜的问题。然而,尽管所有建议都已到位,我仍然找不到解决我当前面临的问题的方法。
你们中的任何人都可以指出我哪里出错了吗?
以下是场景——
我有一个类别类,类别类的每个实例都可以有许多子类别的实例。
我已经使用 @OneToMany 注释设置了关系。然而,当尝试将记录保存到数据库时,我面临 org.hibernate.exception.ConstraintViolationException 异常,报告说外键值不能为 NULL。
请在下面找到类声明
类别.class
import java.util.Date;
import java.util.Set;
import javax.persistence.JoinColumn;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.springframework.context.annotation.Scope;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
@Entity
@Table(name="Category")
@Scope("session")
@EntityListeners(AuditingEntityListener.class)
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Category_Id")
private Long Id;
private String CategoryName; …Run Code Online (Sandbox Code Playgroud) hibernate ×3
java ×3
jpa ×2
spring ×2
one-to-many ×1
persistence ×1
spring-boot ×1
spring-data ×1