-->
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.  [ 4 posts ] 
Author Message
 Post subject: Specifying Ordering from Enumerated Value
PostPosted: Mon Jan 28, 2008 2:24 pm 
Newbie

Joined: Mon Jan 28, 2008 1:50 pm
Posts: 9
Location: EMC
Hi,

I have the following problem with enumerated values and ordering, and can not seem to find an answer. I am trying to use the JPA annotations and queries only.

Hibernate entity manager 3.3.1.GA

I have these two classes:


@Entity
@Table(name="SEVERITY")
public class Severity implements Serializable, Comparable<Severity>{

public enum ImfLevel{
INFO, WARN, SEVERE, CRITICAL;
}

private String severitytext;

@Id
private String severity;

@Enumerated(EnumType.STRING)
@Column(name="SEVERITY", nullable = false, updatable = false)
private ImfLevel level;

@OneToMany(mappedBy="severity")
private Set<Event> events;
.....
}


and

@Entity
@Table(name="EVENT")
public class Event implements Serializable, EventIf {

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private long eventid;

@Column(name = "EVENTTIME")
private Timestamp eventtime;


@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="SEVERITY")
private Severity severity;
. . . .
}

I am trying to query for events (and subset of events), and order them by severity, as defined in the enumerated type.

When I build the following query string I get a list ordered alphabetically.

"from Event event order by event.severity", ok so that seems reasonable.

Now I tried: "from Event event order by event.severity.level", but no luck, still ordered alphabetically. I also tried changing the EnumType to ORDINAL, but that did not change anything.

I have even tried:
"from Event event order by event.severity" and implemented Comparable on Severity, ordering it by level. . . . but still no luck.

Please point me to someplace that can point me in the right direction.

Thanks,

Shannon

_________________
Shannon Moyes Clark


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 4:29 pm 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
I think the problem comes from the fact that the enum values are stored as Strings in the database table. In the end, HQL or the Criteria API just produces plain old SQL, so you have to be able to imagine writing a SQL query that will return results in the order you want. Since they are just plain text in the database, alphabetical is the best ordering it can offer.

I'd recommend storing Integer values in the database instead, so you can easily build a query to return Events in the desired order.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 5:14 pm 
Newbie

Joined: Mon Jan 28, 2008 1:50 pm
Posts: 9
Location: EMC
I think you are correct. I changed the severity class to be:

@Entity
@Table(name="SEVERITY")
public class Severity implements Serializable{

public enum Level{
INFO, WARN, SEVERE, CRITICAL;
}

private String severitytext;

@Id
private String severity;

@Enumerated(EnumType.ORDINAL)
@Column(name="LEVELID", nullable = false, updatable = false)
private Level level;
......

}

Then I put the ordinals (0-3) into the database and it appears to work as expected.

THANKS!!

_________________
Shannon Moyes Clark


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 28, 2008 5:34 pm 
Newbie

Joined: Mon Jan 28, 2008 1:50 pm
Posts: 9
Location: EMC
I think you are correct. I changed the severity class to be:

@Entity
@Table(name="SEVERITY")
public class Severity implements Serializable{

public enum Level{
INFO, WARN, SEVERE, CRITICAL;
}

private String severitytext;

@Id
private String severity;

@Enumerated(EnumType.ORDINAL)
@Column(name="LEVELID", nullable = false, updatable = false)
private Level level;
......

}

Then I put the ordinals (0-3) into the database and it appears to work as expected.

THANKS!!

_________________
Shannon Moyes Clark


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.