强制 Spring Data JPA 对分离实体触发 INSERT 查询而不使用 SELECT
我将 Spring Data JPA 与 Hibernate 结合使用,在其中对分离的实体进行操作。因此,当我保存处于休眠状态的分离实体时,会在内部调用 merge
方法,该方法首先执行 SELECT
语句,然后执行 INSERT
语句。 (该实体不在数据库中,但由于其他原因该实体处于分离状态)
custRepository.save(billingInfo);
假设 billingInfo 是分离实体,当调用 spring jpa save 方法时,它将触发内部生成的 merge
方法首先执行 >SELECT 语句,然后执行 INSERT
语句。
有没有办法告诉 hibernate 仅对分离实体执行 INSERT 查询?
我已经实现了 Persistable 接口,我正在管理实体的状态。
下面是实体
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name= "BLI_INFO")
public class BillingInfo implements Persistable<long> {
@Id
@Column(name = "BLI_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_gen")
@SequenceGenerator(sequenceName = "BLI_ID_SEQ", name = "id_gen", allocationSize = 1)
private long billingId;
...
// rest of the fields
...
@Transient
private boolean isNew = false;
@PrePersist
@PostLoad
void markNotNew() {
this.isNew = false;
}
@Override
public boolean isNew(){
return true;
}
}
I am using Spring Data JPA with Hibernate in which I am operating on the detached entity. So when I save the detached entity hibernated internally call the merge
method which executes first SELECT
statement and then the INSERT
statement. (The entity is not in the DB but for other reason the entity is in detached state)
custRepository.save(billingInfo);
Lets say billingInfo is detached entity, when spring jpa save method is called, it will trigger merge
method internally resulting SELECT
statement execution first and then INSERT
statement.
Is there is a way to tell hibernate to execute only the INSERT
query for the detached entity?
I am already implementing the Persistable
interface which I am managing the state of the entity.
Below is the entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name= "BLI_INFO")
public class BillingInfo implements Persistable<long> {
@Id
@Column(name = "BLI_ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_gen")
@SequenceGenerator(sequenceName = "BLI_ID_SEQ", name = "id_gen", allocationSize = 1)
private long billingId;
...
// rest of the fields
...
@Transient
private boolean isNew = false;
@PrePersist
@PostLoad
void markNotNew() {
this.isNew = false;
}
@Override
public boolean isNew(){
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论