-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: @OrderColumn
PostPosted: Wed Jun 16, 2010 11:00 am 
Newbie

Joined: Wed Jun 16, 2010 10:47 am
Posts: 1
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 ?


Top
 Profile  
 
 Post subject: Re: @OrderColumn
PostPosted: Sat Jul 10, 2010 3:17 am 
Beginner
Beginner

Joined: Wed Aug 24, 2005 5:32 am
Posts: 23
Hi,
Since this is not explicitly documented it is hard to say if this is a bug or intended but this behavior can be explained by the fact that if you use the mappedBy attribute it means that the 'one' side of the one-to-many annotation is no longer the owner of the association and as such it is not allowed to change the order attribute. Another issue is that if you want to include the child entity is more than one ordered association you cannot do it (how do you maintain more than one order on the child entity?).

I'm explaining this behavior in my blog at: http://blog.eyallupu.com/2010/06/hibernate-350-cr-2jpa-20-new-query.html.

Eyal Lupu

_________________
-----------
Eyal Lupu
Blog:http://blog.eyallupu.com/
(Old blog: http://www.jroller.com/eyallupu)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.