-->
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: @Loader annotation on an Entity not working
PostPosted: Sat Jul 28, 2007 5:21 am 
Newbie

Joined: Mon Mar 27, 2006 9:38 am
Posts: 4
My environment:
Windows XP
JDK 1.6
MySQL, version: 5.0.41-community-nt
MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.7
Hibernate Annotations 3.3.0.GA
Hibernate 3.2.4.sp1
Hibernate EntityManager 3.3.1.GA

The problem:
I have a class User that uses an @Loader annotation for specifying native SQL. The relevant portions are copied below:
@Entity
@Table(name = "users", catalog = "cdd", uniqueConstraints = { @UniqueConstraint(columnNames = { "user_name" }) })
@SQLInsert( sql="INSERT INTO cdd.users (full_name, is_administrator, is_disabled, is_readonly, user_name, user_password ) VALUES ( ?, ?, ?, ?, ?, AES_ENCRYPT(?, 'cdd') )")
@SQLUpdate( sql="UPDATE cdd.users SET full_name=?, is_administrator=?, is_disabled=?, is_readonly = ?, user_name=?, user_password=AES_ENCRYPT(?, 'cdd') WHERE UID=?")
@SQLDelete (sql="DELETE FROM cdd.users where UID = ?")
@SQLDeleteAll (sql="DELETE cdd.users")
@NamedNativeQuery (name = "load_user", query = "SELECT UID, full_name, is_administrator, is_disabled, is_readonly, user_name, AES_DECRYPT(user_password, 'cdd') as user_password FROM cdd.users WHERE UID=?", resultClass=User.class)
@Loader (namedQuery = "load_user")
public class User implements java.io.Serializable {
...
}

I am able to use the named query directly from the EntityManager as:
EntityManager em = EntityManagerHelper.getEntityManager();
Query q = em.createNamedQuery("load_user");
q.setParameter(1, 10);
em.getTransaction().begin();
User u = (User) q.getSingleResult();

However, when I "load" an instance of User by calling:
String queryString = "select model from User model";
List<User> users = getEntityManager().createQuery(queryString).getResultList();
the returned instances of User are populated without using the named query identified by the @Loader annotation. I can verify that easily as the named query is basically intended to descrypt the user passwords from the database.

The relevant log entries are:
...
07/28/2007 04:54:59 INFO AnnotationBinder:398 - Binding entity from annotated class: com.epistemic.cdd.db.User
07/28/2007 04:54:59 INFO QueryBinder:127 - Binding named native query: load_user => SELECT UID, full_name, is_administrator, is_disabled, is_readonly, user_name, AES_DECRYPT(user_password, 'cdd') as user_password FROM cdd.users WHERE UID=?
07/28/2007 04:54:59 INFO EntityBinder:420 - Bind entity com.epistemic.cdd.db.User on table users
...
07/28/2007 04:55:01 DEBUG AbstractEntityPersister:2738 - Static SQL for entity: com.epistemic.cdd.db.User
07/28/2007 04:55:01 DEBUG AbstractEntityPersister:2743 - Version select: select UID from cdd.users where UID =?
07/28/2007 04:55:01 DEBUG AbstractEntityPersister:2746 - Snapshot select: select user_.UID, user_.full_name as full2_18_, user_.is_administrator as is3_18_, user_.is_disabled as is4_18_, user_.is_readonly as is5_18_, user_.user_name as user6_18_, user_.user_password as user7_18_ from cdd.users user_ where user_.UID=?
07/28/2007 04:55:01 DEBUG AbstractEntityPersister:2749 - Insert 0: INSERT INTO cdd.users (full_name, is_administrator, is_disabled, is_readonly, user_name, user_password ) VALUES ( ?, ?, ?, ?, ?, AES_ENCRYPT(?, 'cdd') )
07/28/2007 04:55:01 DEBUG AbstractEntityPersister:2750 - Update 0: UPDATE cdd.users SET full_name=?, is_administrator=?, is_disabled=?, is_readonly = ?, user_name=?, user_password=AES_ENCRYPT(?, 'cdd') WHERE UID=?
07/28/2007 04:55:01 DEBUG AbstractEntityPersister:2751 - Delete 0: DELETE cdd.users
07/28/2007 04:55:01 DEBUG AbstractEntityPersister:2755 - Identity insert: INSERT INTO cdd.users (full_name, is_administrator, is_disabled, is_readonly, user_name, user_password ) VALUES ( ?, ?, ?, ?, ?, AES_ENCRYPT(?, 'cdd') )
...
07/28/2007 04:55:01 DEBUG SQL:401 -
select
user0_.UID as UID18_,
user0_.full_name as full2_18_,
user0_.is_administrator as is3_18_,
user0_.is_disabled as is4_18_,
user0_.is_readonly as is5_18_,
user0_.user_name as user6_18_,
user0_.user_password as user7_18_
from
cdd.users user0_
Hibernate:
select
user0_.UID as UID18_,
user0_.full_name as full2_18_,
user0_.is_administrator as is3_18_,
user0_.is_disabled as is4_18_,
user0_.is_readonly as is5_18_,
user0_.user_name as user6_18_,
user0_.user_password as user7_18_
from
cdd.users user0_
07/28/2007 04:55:02 DEBUG AbstractEntityPersister:2031 - Hydrating entity: [com.epistemic.cdd.db.User#1]

Is this a bug?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 3:37 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Weird, I have it working in the unit test suite
org.hibernate.test.annotations.query.QueryAndSQLTest.testEntitySQLOverriding

(Chaos.nickname)

Are you sure you're not making a mistake.
Try to reduce your example as much as possible until you either find a bug on your side on make it a simple unit test. If you still have the issue with a small unit test, attach it to a JIRA issue

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 4:01 pm 
Newbie

Joined: Mon Mar 27, 2006 9:38 am
Posts: 4
Hi Emmanuel,

The only thing that I can see "different" between the test case and my code is an @Column annotation:
@Column(name = "user_password", unique = false, nullable = false, insertable = true, updatable = true)
public String getUserPassword() {
return this.userPassword;
}
Perhaps, we can resolve this pretty quickly. Would it be posible for you to run the test case with a quick modification. Change the underlying column name to, say, nick_name and add the @Column annotation:
@Column (name="nick_name", ...). If that works, then it is clearly something in my environment. If it does not, then the @Column is interfering with the @Loader.

Regards,
Vidur


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 4:07 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yes it works fine
Code:
//$Id: $
package org.hibernate.test.annotations.query;

import java.util.Set;
import java.util.HashSet;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.NamedQuery;
import javax.persistence.NamedNativeQuery;
import javax.persistence.OneToMany;
import javax.persistence.JoinColumn;
import javax.persistence.Column;

import org.hibernate.annotations.SQLInsert;
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.Loader;

/**
* @author Emmanuel Bernard
*/
@Entity
@Table(name="CHAOS")
@SQLInsert( sql="INSERT INTO CHAOS(name, nick_name, size, id) VALUES(upper(?),?,?,?)")
@SQLUpdate( sql="UPDATE CHAOS SET name = upper(?), nick_name = ?, size = ? WHERE id = ?")
@SQLDelete( sql="DELETE CHAOS WHERE id = ?")
@SQLDeleteAll( sql="DELETE CHAOS")
@Loader(namedQuery = "chaos")
@NamedNativeQuery(name="chaos", query="select id, size, name, lower( nick_name ) as nick_name from CHAOS where id= ?", resultClass = Chaos.class)
public class Chaos {
   @Id
   private Long id;
   private Long size;
   private String name;
   @Column(name="nick_name")
   private String nickname;

   @OneToMany
   @JoinColumn(name="chaos_fk")
   @SQLInsert( sql="UPDATE CASIMIR_PARTICULE SET chaos_fk = ? where id = ?")
   @SQLDelete( sql="UPDATE CASIMIR_PARTICULE SET chaos_fk = null where id = ?")
   private Set<CasimirParticle> particles = new HashSet<CasimirParticle>();

   public Long getId() {
      return id;
   }

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

   public Long getSize() {
      return size;
   }

   public void setSize(Long size) {
      this.size = size;
   }

   public String getName() {
      return name;
   }

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

   public String getNickname() {
      return nickname;
   }

   public void setNickname(String nickname) {
      this.nickname = nickname;
   }

   public Set<CasimirParticle> getParticles() {
      return particles;
   }

   public void setParticles(Set<CasimirParticle> particles) {
      this.particles = particles;
   }
}

_________________
Emmanuel


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.