1

我正在尝试在 DocumentModels 之间创建多对多关系,并在关系中添加附加信息(dosIndex)

@Entity
@Table(name = "T_DOCUMENT_MODELS_DMO")
public class TDocumentModelsDmo extends fr.axigate.nx.frontend.server.common.entity.ValidityPeriodEntity implements Serializable
{

    @Id
    @SequenceGenerator(name = "T_DOCUMENT_MODELS_DMO_DMOID_GENERATOR", sequenceName = "T_DMO_ID_SEQ")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "T_DOCUMENT_MODELS_DMO_DMOID_GENERATOR")
    @Column(name = "DMO_ID", precision = 22)
    private Long                        dmoId;

    //Other unrelated members, no reference to TjDocumentSourcesDos
    //constructors, getters and setters without annotations
}

@Entity
@Table(name = "TJ_DOCUMENT_SOURCES_DOS")
public class TjDocumentSourcesDos implements Serializable
{
    @Column(name = "DOS_INDEX", nullable = false, precision = 22)
    private long                    dosIndex; //the additionnal info on the relation

    @EmbeddedId
    private TjDocumentSourcesDosPK  id                  = new TjDocumentSourcesDosPK();

    @ManyToOne
    @MapsId("dosParentId")
    @JoinColumn(name = "DOS_PARENT_ID", nullable = false, insertable = false, updatable = false)
    private TDocumentModelsDmo      TDocumentModelsDmoParent;

    @ManyToOne
    @MapsId("dosSourceId")
    @JoinColumn(name = "DOS_SOURCE_ID", nullable = false, insertable = false, updatable = false)
    private TDocumentModelsDmo      TDocumentModelsDmoSource;

    //constructors, getters and setters without annotations
}

@Embeddable
public class TjDocumentSourcesDosPK implements Serializable
{
    @Column(name = "DOS_PARENT_ID", nullable = false, precision = 22)
    private Long                dosParentId;

    @Column(name = "DOS_SOURCE_ID", nullable = false, precision = 22)
    private Long                dosSourceId;

    //constructors, getters and setters without annotations
    //hashCode and equals implemented
}

我可以在两个表中插入数据,但是当我尝试使用 entityManager 请求它时,我得到了一些奇怪的东西:

Query query = entityManager.createQuery("SELECT dos.TDocumentModelsDmoSource  FROM TDocumentModelsDmo AS dmo, TjDocumentSourcesDos as dos WHERE dmo.dmoId = :modelId AND dos.TDocumentModelsDmoParent = dmo");
query.setParameter("modelId", someData);
ArrayList<TjDocumentSourcesDos> dosList = (ArrayList<TjDocumentSourcesDos>) query.getResultList();

将起作用,而以下将引发异常:QuerySyntaxException: dos.TDocumentModelsDmoSource is not mapped

Query query = entityManager.createQuery("SELECT sources FROM TDocumentModelsDmo AS dmo, TjDocumentSourcesDos as dos, dos.TDocumentModelsDmoSource AS sources WHERE dmo.dmoId = :modelId AND dos.TDocumentModelsDmoParent = dmo");
query.setParameter("modelId", someData);
ArrayList<TjDocumentSourcesDos> dosList = (ArrayList<TjDocumentSourcesDos>) query.getResultList();

sources这可以防止我在 WHERE 条件下使用我的模型的更复杂的请求。

我尝试referencedColumnName = "DMO_ID"在我的两个 JoinColumn 注释中添加一个,但我仍然得到同样的错误

4

0 回答 0