I managed to get it to poulate the index colums, by removing mapped by in the @OneToMany side. Removing this mean that it creates 2 extra tables (1 per relation)
The problem i am now having is getting Hibernate to query the data, as hibernate is forgetting to include one of the tables created by removing mappedBy.
the current set up of the entities is
ExpressionHierarchy entity
Code:
@Entity
@Table(name = "expressionhierarchy")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DISCRIMINATOR",
discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("expression")
public class Expression implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1091620752774725110L;
private int mId;
private ExpressionType mType;
private String mExpr;
private String mDescription;
private List<ExpressionTarget> mExpressionTargets = new ArrayList<ExpressionTarget>();
public enum ExpressionType {
REGEX, EQUATION, SET
}
@Id
public int getId() {
return mId;
}
public void setId(final int pId) {
mId = pId;
}
public String getExpr() {
return mExpr;
}
public void setExpr(final String pExpr) {
mExpr = pExpr;
}
public ExpressionType getType() {
return mType;
}
public void setType(final ExpressionType pType) {
mType = pType;
}
public String getDescription() {
return mDescription;
}
public void setDescription(final String pDescription) {
mDescription = pDescription;
}
@OneToMany(cascade = CascadeType.ALL)//, mappedBy = "expression")
@IndexColumn(name = "expression_index", base=1)
public List<ExpressionTarget> getExpressionTargets() {
return mExpressionTargets;
}
public void setExpressionTargets(final List<ExpressionTarget> pExpressionTargets) {
mExpressionTargets = pExpressionTargets;
}
}
ExpressionTarget entity
Code:
@Entity
public class ExpressionTarget implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1658544469715177717L;
private int mId;
private int mRole;
private TargetEntity mTargetEntity;
private Expression mExpression;
public ExpressionTarget() { }
public ExpressionTarget(int pPk, int pRole, TargetEntity pTargetEntity, Expression pExpression) {
setId(pPk);
setRole(pRole);
setTargetEntity(pTargetEntity);
setExpression(pExpression);
}
@Id
public int getId() {
return mId;
}
public void setId(final int pId) {
mId = pId;
}
public int getRole() {
return mRole;
}
public void setRole(final int pRole) {
mRole = pRole;
}
@ManyToOne
@JoinColumn(name = "targetEntity_id")
public TargetEntity getTargetEntity() {
return mTargetEntity;
}
public void setTargetEntity(final TargetEntity pTargetEntity) {
this.mTargetEntity = pTargetEntity;
}
@ManyToOne
@JoinColumn(name = "expression_id")
public Expression getExpression() {
return mExpression;
}
public void setExpression(final Expression pExpression) {
mExpression = pExpression;
}
}
TargetEntity entity
Code:
@Entity
public class TargetEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1777950853877950863L;
private int mId;
private Class mClazz;
private int mPk;
private String mAttrName;
private Target mTarget;
private List<ExpressionTarget> mExpressionTargets = new ArrayList<ExpressionTarget>();
public String getAttrName() {
return mAttrName;
}
public void setAttrName(String attrName) {
mAttrName = attrName;
}
@Basic(optional = false)
public Class getClazz() {
return mClazz;
}
public void setClazz(Class pClazz) {
mClazz = pClazz;
}
@Id
public int getId() {
return mId;
}
public void setId(int pId) {
mId = pId;
}
public int getPk() {
return mPk;
}
public void setPk(int pPk) {
mPk = pPk;
}
@Transient
public Target getTarget() {
if(mTarget == null)
mTarget = TargetFactory.createTarget(this);
return mTarget;
}
@OneToMany(cascade = CascadeType.ALL)//, mappedBy = "targetEntity")
@IndexColumn(name = "target_index", base=1)
public List<ExpressionTarget> getExpressionTargets() {
return mExpressionTargets;
}
public void setExpressionTargets(final List<ExpressionTarget> pExpressionTargets) {
this.mExpressionTargets = pExpressionTargets;
}
}
The simple query that i am trying to perform (worked when relations used mappedBy) is
Code:
Query tmpQuery = mEntityManager.createQuery("SELECT e.expression FROM TargetEntity " +
"t INNER JOIN t.expressionTargets e " +
"WHERE t.id = :pk AND e.expression.class = Rule").setParameter("pk", 1);
This produces the following SQL
Quote:
select expression3_.id as id1806_, expression3_.type as type1806_, expression3_.description as descript4_1806_, expression3_.expr as expr1806_, expression3_.DISCRIMINATOR as DISCRIMI1_1806_ from TargetEntity targetenti0_ inner join TargetEntity_ExpressionTarget expression1_ on targetenti0_.id=expression1_.TargetEntity_id inner join ExpressionTarget expression2_ on expression1_.expressionTargets_id=expression2_.id inner join expressionhierarchy expression3_ on expression2_.expression_id=expression3_.id where targetenti0_.id=? and expression3_.DISCRIMINATOR='rule'
Hibernate did not include the join needed between ExpressionTarget and expressionhierarchy_ExpressionTarget (this is an auto generated table, not a concreate entity). So do i have to actually set the Expression in ExpressionTarget with a .setExpression as in my opinion it would be bad in database terms (redundant data)?
The correct query should of been this
Quote:
select
expression3_.id as id1753_,
expression3_.type as type1753_,
expression3_.description as descript4_1753_,
expression3_.expr as expr1753_, expression3_.DISCRIMINATOR as DISCRIMI1_1753_
from TargetEntity targetenti0_
inner join TargetEntity_ExpressionTarget expression1_ on targetenti0_.id=expression1_.TargetEntity_id
inner join ExpressionTarget expression2_ on expression1_.expressionTargets_id=expression2_.id
inner join expressionhierarchy_ExpressionTarget expHiTar on expression2_.id=expHiTar.expressiontargets_id
inner join expressionhierarchy expression3_ on expHiTar.expressionhierarchy_id=expression3_.id
where targetenti0_.id=1 and expression3_.DISCRIMINATOR='rule'
Can anyone point out where i have gone wrong ?
Cheers,
Andy