I have some trouble with the jpa 2 annotation @OrderColumn.
While understanding jpa2 and hibernate docs
http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/ @OrderColumn will create a column in the "child" table for ordering lists.......
I have an example:
Code:
@Entity
@Table(name = "TEMPLATE_QUESTIONAIRE")
public class TemplateQuestionaire implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(length = 50)
@NotNull
private String templateQuestionaireId;
@Column(nullable = false, length = 2)
@NotNull
@Min(1)
@Max(10)
private int mainTariffType;
@Column(unique = true, nullable = false, length = 50)
@NotNull
private String questionaireName;
@Column(nullable = false, length = 250)
@NotNull
private String questionaireDescription;
@OneToMany
@OrderColumn(name = "viewpoint")
private List<TemplateQuestion> questions = new ArrayList<TemplateQuestion>();
public TemplateQuestionaire()
{
super();
}
public TemplateQuestionaire(String templateQuestionaireId, int mainTariffType, String questionaireName, String questionaireDescription)
{
super();
this.templateQuestionaireId = templateQuestionaireId;
this.mainTariffType = mainTariffType;
this.questionaireName = questionaireName;
this.questionaireDescription = questionaireDescription;
}
/**
* @return the templateQuestionaireId
*/
public String getTemplateQuestionaireId()
{
return templateQuestionaireId;
}
/**
* @param templateQuestionaireId
* the templateQuestionaireId to set
*/
public void setTemplateQuestionaireId(String templateQuestionaireId)
{
this.templateQuestionaireId = templateQuestionaireId;
}
/**
* @return the mainTariffType
*/
public int getMainTariffType()
{
return mainTariffType;
}
/**
* @param mainTariffType
* the mainTariffType to set
*/
public void setMainTariffType(int mainTariffType)
{
this.mainTariffType = mainTariffType;
}
/**
* @return the questionaireName
*/
public String getQuestionaireName()
{
return questionaireName;
}
/**
* @param questionaireName
* the questionaireName to set
*/
public void setQuestionaireName(String questionaireName)
{
this.questionaireName = questionaireName;
}
/**
* @return the questionaireDescription
*/
public String getQuestionaireDescription()
{
return questionaireDescription;
}
/**
* @param questionaireDescription
* the questionaireDescription to set
*/
public void setQuestionaireDescription(String questionaireDescription)
{
this.questionaireDescription = questionaireDescription;
}
/**
* @return the questions
*/
public List<TemplateQuestion> getQuestions()
{
return questions;
}
/**
* @param questions
* the questions to set
*/
public void setQuestions(List<TemplateQuestion> questions)
{
this.questions = questions;
}
}
and
Code:
@Entity
@Table(name = "TEMPLATE_QUESTION")
public class TemplateQuestion implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(length = 50)
@NotNull
private String templateQuestionId;
@ManyToOne
private TemplateQuestionaire templateQuestionaire;
@ManyToOne(targetEntity = TemplateGroup.class, fetch=FetchType.EAGER , cascade={CascadeType.DETACH,CascadeType.REFRESH})
private TemplateGroup templateGroup;
public TemplateQuestion()
{
}
public TemplateQuestion(String templateQuestionId, TemplateQuestionaire templateQuestionaire, TemplateGroup templateGroup)
{
super();
this.templateQuestionId = templateQuestionId;
this.templateQuestionaire = templateQuestionaire;
this.templateGroup = templateGroup;
}
/**
* @return the templateQuestionId
*/
public String getTemplateQuestionId()
{
return templateQuestionId;
}
/**
* @param templateQuestionId the templateQuestionId to set
*/
public void setTemplateQuestionId(String templateQuestionId)
{
this.templateQuestionId = templateQuestionId;
}
/**
* @return the templateGroup
*/
public TemplateGroup getTemplateGroup()
{
return templateGroup;
}
/**
* @param templateGroup the templateGroup to set
*/
public void setTemplateGroup(TemplateGroup templateGroup)
{
this.templateGroup = templateGroup;
}
/**
* @param templateQuestionaire the templateQuestionaire to set
*/
public void setTemplateQuestionaire(TemplateQuestionaire templateQuestionaire)
{
this.templateQuestionaire = templateQuestionaire;
}
/**
* @return the templateQuestionaire
*/
public TemplateQuestionaire getTemplateQuestionaire()
{
return templateQuestionaire;
}
}
running this example will do the following: hibernates creates 3 tables: TemplateQuestionaire, TemplateQuestion and TemplateQuestionaire_TemplateQuestion. Where TemplateQuestionaire_TemplateQuestion is having the viewpoint ordercolumn with right setted values.
if i change
Code:
@OneToMany
@OrderColumn(name = "viewpoint")
private List<TemplateQuestion> questions = new ArrayList<TemplateQuestion>();
to
Code:
@OneToMany(mappedBy="templateQuestionaire")
@OrderColumn(name = "viewpoint")
private List<TemplateQuestion> questions = new ArrayList<TemplateQuestion>();
hibernate will generate two tables: TemplateQuestionaire and TemplateQuestion , where TemplateQuestion has got the viewpoint ordercolumn. .... Thats no better, but the ordercolumn has got set no values ....
Whats wrong ? You can help ?