-->
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.  [ 1 post ] 
Author Message
 Post subject: Adding then Removing Projection causes ClassCastException
PostPosted: Sat Mar 04, 2006 7:18 pm 
Beginner
Beginner

Joined: Fri Jul 30, 2004 2:53 pm
Posts: 33
Location: Washington, DC
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.2

Mapping documents:

User: (private variables and setters omitted)
Code:
@Entity
@Table(name="users")
@SequenceGenerator(name="seq_users",sequenceName="seq_users")
public class User extends BaseEntity {

    @Override
    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_users")
    public Long getId() { return super.getId(); }
   
    @Column(nullable=false,unique=true,length=32)
    public String getUsername() { return username; }

}


LoginAttempt: (private variables and setters omitted)
Code:
@Entity
@Table(name="login_attempts")
@SequenceGenerator(name="seq_login_attempts",sequenceName="seq_login_attempts")
public class LoginAttempt extends BaseEntity {

    @Override
    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_login_attempts")
    public Long getId() { return super.getId(); }

    @ManyToOne()
    @Column(name="user_id",nullable=false)
    public User getUser() {
        return user;
    }
   
    @Column(name="attempted_at")
    public Date getAttemptedAt() {
        return attemptedAt;
    }

    @Column(nullable=false)
    public boolean isSuccess() {
        return success;
    }

}


Code between sessionFactory.openSession() and session.close():

Code:
Criteria user = session.createCriteria(User.class);
user.setProjection(Projections.rowCount());
System.out.println("User count: "+user.list().get(0));
user.setProjection(null);

List users = user.list();
System.out.println(users.get(0).getClass());
System.out.println((User)users.get(0));

Criteria login = session.createCriteria(LoginAttempt.class);
login.setProjection(Projections.rowCount());
System.out.println("Login count: "+login.list().get(0));
login.setProjection(null);

List logins = login.list();
System.out.println(logins.get(0).getClass());
System.out.println((LoginAttempt)logins.get(0));


Name and version of the database you are using:

RDBMS: Oracle, version: Oracle9i Enterprise Edition Release 9.2.0.6.0 - Production
JDBC driver: Oracle JDBC driver, version: 10.1.0.2.0

When I run the code above, the line where I try to cast to LoginAttempt throws a class cast exception. Here is what the output is:

User count: 9
class my.package.model.User
my.package.model.User[1]
Login count: 19
class [Ljava.lang.Object;

As you can see, the User works ok, I can cast it to a User object, even after using the projection. But the LoginAttempt, I cannot. Is this a bug or can I not use projections in this way? Why does it work with User but not LoginAttempt?


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

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.