在使用复合主键请求ManyToMany关系时出现奇怪的行为

时间:2013-08-29 16:40:11

标签: java hibernate hibernate-mapping composite-key

我正在尝试在DocumentModels之间创建一个ManyToMany关系,在关系中有一个额外的信息(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();

这使我无法执行更复杂的请求,我会在WHERE条件中使用sources模型。

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

0 个答案:

没有答案