休眠搜索:在复合键EmbeddedId中搜索

问题描述

我将类import QtQuick 2.11 import QtQuick.Window 2.11 import QtGraphicalEffects 1.0 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") Image { id: img anchors.fill: parent source: "your_non_fitting_image.png" fillMode: Image.PreserveAspectFit visible: false } Item { id: mask anchors.fill: img visible: false Rectangle { color: "white" radius: 20 anchors.centerIn: parent width: img.paintedWidth height: img.paintedHeight } } OpacityMask { anchors.fill: img source: img maskSource: mask } } 定义为嵌入式id类。

我定义了类CKey,并为复合主键使用了双向字符串字段桥。

我将类BEntity和关系AEntity定义为ManyToOne

BEntity

然后,我尝试对Entity类@Entity @Indexed public class AEntity { @ManyToOne(optional = false) @IndexedEmbedded(depth = 1,includeEmbeddedobjectId = true) @JoinColumns(value = { @JoinColumn(name = "fk_fId",referencedColumnName = "fId"),@JoinColumn(name = "fk_sId",referencedColumnName = "sId") },foreignKey = @ForeignKey(name = "fk_sub")) private BEntity bEntity; } @Entity @Indexed public class BEntity { @EmbeddedId @IndexedEmbedded @FieldBridge(impl = CompositeIdBridge.class) private CKey cKey; } @Embeddable @Indexed public class CKey { @Field private String fId; @Field private String sId; } public class CompositeIdBridge implements TwoWayStringBridge { @Override public String objectToString(Object object) { return String.format("%s.%s",(CKey) object.getFId(),(CKey) object.getSId()); } @Override public Object stringToObject(String stringValue) { String[] compositeIdProperties = stringValue.split("\\."); return new CKey(compositeIdProperties[1],compositeIdProperties[2]); } } 进行休眠搜索,但是遇到了此异常:

在AEntity中找不到字段bEntity.cKey.fId

AEntity

解决方法

您在@IndexedEmbedded(depth = 1)中的AEntity明确要求仅嵌入深度为1的字段。 bEntity.cKey相对于bEntity的深度为1,但是bEntity.cKey.fIdbEntity.cKey.sId的深度为2。

您应该增加深度:

@Entity @Indexed
public class AEntity {
    @ManyToOne(optional = false)
    @IndexedEmbedded(depth = 2,includeEmbeddedObjectId = true)
    @JoinColumns(value = { @JoinColumn(name = "fk_fId",referencedColumnName = "fId"),@JoinColumn(name = "fk_sId",referencedColumnName = "sId") },foreignKey = @ForeignKey(name = "fk_sub"))
    private BEntity bEntity;
}

...或者您应该明确包含以下字段:

@Entity @Indexed
public class AEntity {
    @ManyToOne(optional = false)
    @IndexedEmbedded(depth = 1,includeEmbeddedObjectId = true,includePaths = {"cKey.fId","cKey.sId"})
    @JoinColumns(value = { @JoinColumn(name = "fk_fId",foreignKey = @ForeignKey(name = "fk_sub"))
    private BEntity bEntity;
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...