JPA持久性@Basic(optional = false)和@Column(nullable = false)JPA持久性之间有什么区别?
我有2张桌子:Order [OrderId(PK), OrderShipmentCode, ...]和Shipment[ShipmentId(PK), ShipmentCode, ...].
在Order课堂上,我声明shipment了如下字段:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "OrderShipmentCode", referencedColumnName = "ShipmentCode", insertable = false, updatable = false, nullable = false)
private Shipment shipment;
Run Code Online (Sandbox Code Playgroud)
当我得到列表时Order,Shipment也会加载(我看到很多单独的SELECT查询).但是我希望它Shipment是懒惰的,而不是与它一起提取Order.
对于其他表,如果引用的列是主键,则结果与预期一致(使用延迟加载).在我的情况下,ShipmentCode不是Shipment表的主键,并且Hibernate不使用延迟加载.
你能告诉我如何实现这个目标吗?
编辑: 查询代码如下:
Criteria criteria = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(Order.class);
List result = criteria.list();
Run Code Online (Sandbox Code Playgroud)
SQL查询是:1 Order表的SELECT语句和一堆SELECT语句Shipment
我有两个实体AuthorizationPosition和ProductAttributes一个WildflyEE项目.两者之间的映射如下:
AuthorizationPosition:
@javax.persistence.ManyToOne(fetch = javax.persistence.FetchType.LAZY , optional=false, cascade = {javax.persistence.CascadeType.ALL})
@javax.persistence.JoinColumn(name = "PRODUCT_ATTRIBUTES_ID", referencedColumnName="PRODUCT_ATTRIBUTES_ID")
protected org.example.persistence.entity.ProductAttributes productAttributes;
Run Code Online (Sandbox Code Playgroud)
产品属性:
@javax.persistence.OneToMany(
targetEntity=org.example.persistence.entity.AuthorizationPosition.class,
mappedBy="productAttributes",
fetch=javax.persistence.FetchType.LAZY
, cascade = javax.persistence.CascadeType.ALL
, orphanRemoval = true )
protected java.util.Set<org.example.persistence.entity.AuthorizationPosition> authorizationPositionsByProductAttributes;
Run Code Online (Sandbox Code Playgroud)
我把所有的关系,在两个实体,并呼吁em.persist()对ProductAttributes(作为一个更大结构的一部分),这是这里的怪错误出现(在这个问题结束全堆栈跟踪):
java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : org.example.persistence.entity.AuthorizationPosition.productAttributes -> org.example.persistence.entity.ProductAttributes
Run Code Online (Sandbox Code Playgroud)
我错过了什么或Hibernate(hibernate-core-5.3.1.Final)真的试图坚持其他方式?这不应该是一个问题,尽管CascadeType.ALL在所有关系映射中都存在这个问题吗?
完整的堆栈跟踪:
java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value …Run Code Online (Sandbox Code Playgroud)