我有ParentRepository
为ParentEntity
ies。Child1Entity
和Child2Entity
继承自ParentEntity
。Child1Entity
与OutputFileEntity
之间存在@OneToOne
惰性关联。ParentEntity
只有id
字段。
是否可以以某种方式获取outputFile
?我需要存储库返回ParentEntity
类型,我需要保持与OutputFileEntity
的惰性关联
堆栈:Kotlin,MySQL,Hibernate,Postgres
它在@Test
中的H2上运行良好,但在Postresql上我有错误
@Repository
interface ParentRepository : CrudRepository<ParentEntity, Long> {
@Query(
"""
SELECT DISTINCT p FROM ParentEntity p
LEFT JOIN Child1Entity ch1 ON p.id = ch1.id
LEFT JOIN Child2Entity ch2 ON p.id = ch2.id
LEFT JOIN ch1.outputFile outputFile
"""
)
fun getParents(pageable: Pageable): Page<ParentEntity>
}
错误是只在Postresql当尝试访问outputFile
:
org.hibernate.LazyInitializationException: could not initialize proxy [xxxxxx.OutputFileEntity#302] - no Session
更多信息
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@SequenceGenerator(name = ParentEntity.GENERATOR_NAME, sequenceName = ParentEntity.SEQUENCE_NAME, allocationSize = 1)
abstract class ParentEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = GENERATOR_NAME)
var id: Long = 0
// static fields
companion object {
const val SEQUENCE_NAME = "generic_parent_id_seq"
const val GENERATOR_NAME = "${SEQUENCE_NAME}_gen"
}
}
@Entity
@Table(name = TABLE_NAME)
class Child1Entity(
val abc1: String? = null,
@OneToOne(
cascade = [CascadeType.ALL],
fetch = FetchType.LAZY,
orphanRemoval = true
)
val outputFile: OutputFileEntity,
) : ParentEntity() {
// static fields
companion object {
const val TABLE_NAME = "child1"
}
}
@Entity
@Table(name = TABLE_NAME)
class Child2Entity(
val abc2: String? = null,
) : ParentEntity() {
// static fields
companion object {
const val TABLE_NAME = "child2"
}
}
当我将FETCH
添加到存储库时:
SELECT DISTINCT p FROM ParentEntity p
LEFT JOIN Child1Entity ch1 ON p.id = ch1.id
LEFT JOIN Child2Entity ch2 ON p.id = ch2.id
LEFT JOIN FETCH ch1.outputFile outputFile
我得到了这个错误
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=outputFile,role=xxxxx.Child1Entity.outputFile,tableName=output_file,tableAlias=outputfile3_,origin=child1,columns={child1_.output_file_id,className=xxxx.OutputFileEntity}}]
at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:230)
at org.hibernate.hql.internal.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:1039)
at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:807)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:703)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:339)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:287)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192)
所以我尝试添加子实体选择列表
SELECT p, ch1, ch2 FROM ParentEntity p
LEFT JOIN Child1Entity ch1 ON p.id = ch1.id
LEFT JOIN Child2Entity ch2 ON p.id = ch2.id
LEFT JOIN FETCH ch1.outputFile outputFile
但得到
.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [xxxxx.ParentEntity]
at org.hibernate.internal.AbstractSharedSessionContract.resultClassChecking(AbstractSharedSessionContract.java:894)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:849)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:114)
1条答案
按热度按时间qgelzfjb1#
“客户端”函数上缺少
@Transactional
注解🤯