Strange behaviour when requesting on a ManyToMany relation with composite
PrimaryKey
I am trying to create a ManyToMany relation between DocumentModels, with
an additionnal information in the relation (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
}
I can insert datas in both tables, but when I try to request it using an
entityManager, i get something strange :
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();
will work, while the following will throw an exception :
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();
This prevents me from doing more complicated requests where I would use my
sources models in the WHERE condition.
I tried adding a referencedColumnName = "DMO_ID" in both my JoinColumn
annotations, but I still get the same error
No comments:
Post a Comment