hsql/jdbc 等同于 JPA Detach api 是什么?它到底是做什么的?
what is the hsql/jdbc equivalent to JPA Detach api? what does it exactly do?
我正在使用 JDBC 将 JPA api 分离到普通 SQL,我发现很难理解 EntityManager.detach(someTask) 的概念。
我尝试使用 delete 查询进行分离,但它似乎立即从数据库中删除了整个记录。
@Entity
@Table(name = "TASK", indexes = {@Index(name = "RIO", columnList = "priority", unique = false),
@Index(name = "EXP", columnList = "expiry", unique = false),
@Index(name = "STA", columnList = "taskStatus", unique = false),
@Index(name = "CAT", columnList = "category", unique = false),
@Index(name = "NEXTTRY", columnList = "nextTry", unique = false)})
public class TaskEntity {
@Version
private int version;
@Basic
@Column(length = Integer.MAX_VALUE, columnDefinition = "varchar(" +
Integer.MAX_VALUE + ")")
private String taskId;
@Basic
private String category;
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "KEY")
@CollectionTable(name = "TASKPROPERTIES", foreignKey = @ForeignKey(
name = "TASK_ID_FK",
foreignKeyDefinition = "FOREIGN KEY (TASKENTITY_ID) REFERENCES TASK (ID) ON DELETE CASCADE"))
@Column(length = Integer.MAX_VALUE, columnDefinition = "varchar(" +
Integer.MAX_VALUE + ")")
private Map<String, String> TaskProperties;
@Basic
@Column(length = Integer.MAX_VALUE, columnDefinition = "varchar(" +
Integer.MAX_VALUE + ")")
private String destination;
@Enumerated(EnumType.STRING)
private TaskStatus taskStatus;
@Basic
private String type;
@Basic
private Long expiry;
@Basic
private Long nextTry;
@Basic
private Integer retries;
@Basic
private Integer priority;
//Setters and Getters
//Equals and HashCode
}
很难理解 detach 和 remove.So 之间的区别 HSQL/SQL 中 detach 的等效查询是什么?
为了理解detach
,您必须首先熟悉proxy 设计模式。当您保留或获取实体时,ORM 会为您提供一个跟踪字段修改并为您提供延迟加载的代理。
当您分离一个实体时,您告诉 ORM 您不希望它再跟踪该对象实例。因此,您在分离实体实例后对其所做的任何更改都不会反映在数据库中。
换句话说,detach()
纯粹是 ORM 的一个功能,与数据库无关,这就是为什么你找不到和 SQL 等价的原因——仅仅因为有 none .
我正在使用 JDBC 将 JPA api 分离到普通 SQL,我发现很难理解 EntityManager.detach(someTask) 的概念。
我尝试使用 delete 查询进行分离,但它似乎立即从数据库中删除了整个记录。
@Entity
@Table(name = "TASK", indexes = {@Index(name = "RIO", columnList = "priority", unique = false),
@Index(name = "EXP", columnList = "expiry", unique = false),
@Index(name = "STA", columnList = "taskStatus", unique = false),
@Index(name = "CAT", columnList = "category", unique = false),
@Index(name = "NEXTTRY", columnList = "nextTry", unique = false)})
public class TaskEntity {
@Version
private int version;
@Basic
@Column(length = Integer.MAX_VALUE, columnDefinition = "varchar(" +
Integer.MAX_VALUE + ")")
private String taskId;
@Basic
private String category;
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "KEY")
@CollectionTable(name = "TASKPROPERTIES", foreignKey = @ForeignKey(
name = "TASK_ID_FK",
foreignKeyDefinition = "FOREIGN KEY (TASKENTITY_ID) REFERENCES TASK (ID) ON DELETE CASCADE"))
@Column(length = Integer.MAX_VALUE, columnDefinition = "varchar(" +
Integer.MAX_VALUE + ")")
private Map<String, String> TaskProperties;
@Basic
@Column(length = Integer.MAX_VALUE, columnDefinition = "varchar(" +
Integer.MAX_VALUE + ")")
private String destination;
@Enumerated(EnumType.STRING)
private TaskStatus taskStatus;
@Basic
private String type;
@Basic
private Long expiry;
@Basic
private Long nextTry;
@Basic
private Integer retries;
@Basic
private Integer priority;
//Setters and Getters
//Equals and HashCode
}
很难理解 detach 和 remove.So 之间的区别 HSQL/SQL 中 detach 的等效查询是什么?
为了理解detach
,您必须首先熟悉proxy 设计模式。当您保留或获取实体时,ORM 会为您提供一个跟踪字段修改并为您提供延迟加载的代理。
当您分离一个实体时,您告诉 ORM 您不希望它再跟踪该对象实例。因此,您在分离实体实例后对其所做的任何更改都不会反映在数据库中。
换句话说,detach()
纯粹是 ORM 的一个功能,与数据库无关,这就是为什么你找不到和 SQL 等价的原因——仅仅因为有 none .