-->
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: auto query date property using between
PostPosted: Wed Dec 28, 2011 11:50 am 
Newbie

Joined: Wed Dec 28, 2011 11:35 am
Posts: 2
for example I have pojo, I hope I can query Student with

Code:
Student student = new Student();
student.setBirthDayStart(toDate(“2010”));
student.setBirthDayEnd(toDate(“2012”));

hibernateSession.queryByExample(student);


Then the hibernate will execute the below HQL:

Code:
From Student Where birthDay >= :birthDayStart and birthday < :birthDayEnd


My problem is "how to"? I want resolve it through annotations like below:
@Column(name = "birth_day", operation = ">="). Some tools can auto generate the HQL

PS: if the “name” property is set, the HQL will append( same to every string type property):
“ and name like %:NameValue%”
If the “id” property is set, the HQL will append(same to every int/long/number property):
“ and id = :idValue”

Focuus the below line in the POJO
@Column(name = "birth_day", operation = ">=")
public Date getBirthDayStart() {
}


Code:
@Entity
@Table(appliesTo = "t_student")
public class Student {
   private int id;
   private String name;
   private Date birthDay;
   private Date birthDayStart;
   private Date birthDayEnd;

   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public Date getBirthDay() {
      return birthDay;
   }

   public void setBirthDay(Date birthDay) {
      this.birthDay = birthDay;
   }

   @Transient
   @Column(name = "birth_day", operation = ">=")
   public Date getBirthDayStart() {
      return birthDayStart;
   }

   public void setBirthDayStart(Date birthDayStart) {
      this.birthDayStart = birthDayStart;
   }

   @Transient
   @Column(name = "birth_day", operation = "<")
   public Date getBirthDayEnd() {
      return birthDayEnd;
   }

   public void setBirthDayEnd(Date birthDayEnd) {
      this.birthDayEnd = birthDayEnd;
   }
}


Top
 Profile  
 
 Post subject: Re: auto query date property using between
PostPosted: Wed Dec 28, 2011 12:12 pm 
Newbie

Joined: Wed Dec 28, 2011 11:35 am
Posts: 2
I do not want to use Criteria, i hope the default behavior of hibernate will be:

you can query any POJO use a method like queryByExample:
1, if you set a int/long/nubmer type property, the the method will dynamic generate the HQL :
Code:
From XXXPojo Where intPropertyName = :intPropertyValue


2, if you set a string property, the method will auto append:
Code:
and stringPropertyName like :stiringPropertyValue


3,if POJO have a date/dateTime property, such as birthday, i can define the orther two property like
birhtdayStart, birthdayEnd, and annotate them with
Code:
@Transient
@Column(name="birthday", operation=">=")
private Date birhtdayStart;

@Transient
@Column(name="birthday", operation="<")
private Date birthdayEnd;

the method will auto append like
Code:
birthday >= :birthdayStart and birthday < :birthdayEnd


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.