-->
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.  [ 8 posts ] 
Author Message
 Post subject: pg-uuid with String getter and setter
PostPosted: Thu Sep 29, 2016 1:38 am 
Newbie

Joined: Thu Sep 29, 2016 1:11 am
Posts: 4
Hi,

I am using hibernate 5 and below is my issue.
I have id field in postgres db as uuid and have corresponding POJO as UUID as variable and getter/setter as String. I have following POjO :

Code:
public class LoginAuditInfo implements LoginAudit {
   
   /**
    * Automatically generated Id7
    */

   
   private UUID Id;

   public String getId() {
      return String.valueOf(this.Id);
//         return this.Id;
   }

   public void setId(String id) {
      Id = UUID.fromString(id);
//      Id = id;
   }
   


   private String userId;

   /**
    * Session Id
    */
   private String sessionId;

   /**
    * Time User Logged In
    */
   private Date LogInTime;

   /**
    * Time User Logged Out
    */
   private Date LogOutTime;

   private Date loginFailTime;

   /**
    * @return the userId
    */
   public String getUserId() {
      return userId;
   }

   /**
    * @param userId
    *            the userId to set
    */
   public void setUserId(String userId) {
      this.userId = userId;
   }

   /**
    * @return the sessionId
    */
   public String getSessionId() {
      return sessionId;
   }

   /**
    * @param sessionId
    *            the sessionId to set
    */
   public void setSessionId(String sessionId) {
      this.sessionId = sessionId;
   }

   /**
    * @return the logInTime
    */
   public Date getLogInTime() {
      return LogInTime;
   }

   /**
    * @param logInTime
    *            the logInTime to set
    */
   public void setLogInTime(Date logInTime) {
      LogInTime = logInTime;
   }

   /**
    * @return the logOutTime
    */
   public Date getLogOutTime() {
      return LogOutTime;
   }

   /**
    * @param logOutTime
    *            the logOutTime to set
    */
   public void setLogOutTime(Date logOutTime) {
      LogOutTime = logOutTime;
   }

   /**
    * @return the logFailTime
    */
   public Date getLoginFailTime() {
      return loginFailTime;
   }

   /**
    * @param sets
    *            the logFailTime
    */
   public void setLoginFailTime(Date _loginFailTime) {
      loginFailTime = _loginFailTime;
   }

}

The following is my hbm :

Code:
[color=#404040]<hibernate-mapping>
   
   <class name="LoginAuditInfo" table="user1">
      <id name="Id" type="pg-uuid" >
         <column name="Id" sql-type="uuid" not-null="true" />
         <generator class="uuid2"></generator>
      </id>

      <property name="userId"></property>
      <property name="sessionId">
         <column name="session_id"></column>
      </property>
      <property name="LogOutTime">
         <column name="log_out_time"></column>
      </property>
      <property name="loginFailTime">
         <column name="login_fail_time"></column>
      </property>
      <property name="LogInTime">
         <column name="log_in_time"></column>
      </property>
   </class>

</hibernate-mapping> 
[/color]


While running the code I got the following error:

Code:
Caused by: IllegalArgumentException occurred while calling setter for property [LoginAuditInfo.Id (expected type = java.lang.String)]; target = [com.accenture.amap.LoginAuditInfo@10667848], property value = [8174ff23-9f62-4f21-9520-b1aff9318d06]
   at org.hibernate.property.access.spi.SetterMethodImpl.set(SetterMethodImpl.java:99)
   at org.hibernate.tuple.entity.AbstractEntityTuplizer.setIdentifier(AbstractEntityTuplizer.java:258)
   at org.hibernate.persister.entity.AbstractEntityPersister.setIdentifier(AbstractEntityPersister.java:4651)
   at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:172)
   at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
   at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
   at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
   at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
   at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
   at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
   at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:667)
   at org.hibernate.internal.SessionImpl.save(SessionImpl.java:659)
   at org.hibernate.internal.SessionImpl.save(SessionImpl.java:654)
   at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:619)
   at org.springframework.orm.hibernate5.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:615)
   at org.springframework.orm.hibernate5.HibernateTemplate.doExecute(HibernateTemplate.java:340)
   ... 14 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.hibernate.property.access.spi.SetterMethodImpl.set(SetterMethodImpl.java:44)
   ... 29 more


But the same configuration works fine with annotation. I do not want to use annotation as I have support for multiple databases. Is there any way to use the above configuration or it is necessary to have getter/setter also as UUID. Please suggest.

Thanks & Regards,
Ankit


Top
 Profile  
 
 Post subject: Re: Issue with uuid type with postgres
PostPosted: Thu Sep 29, 2016 2:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The pg-uuid is meant to be used with java.util.UUID entity attributes. The getter and setter should match the attribute type as well, you are violating the Java Beans contract here, therefore you are getting this exception. Hibernate does not require getters and setters, but if you provide them,they must follow the Java Beans spec.

Therefore, you only have to rename the get/set methods:

private UUID Id;

Code:
public String getIdAsString() {
    return String.valueOf(this.Id);
}

public void setIdAsString(String id) {
    Id = UUID.fromString(id);
}


public String getId() {
return String.valueOf(this.Id);
// return this.Id;
}

public void setId(String id) {
Id = UUID.fromString(id);
// Id = id;
}


Top
 Profile  
 
 Post subject: Re: pg-uuid with String getter and setter
PostPosted: Thu Sep 29, 2016 3:26 am 
Newbie

Joined: Thu Sep 29, 2016 1:11 am
Posts: 4
Is it possible to have db field as uuid and java field as String and getter/setter as String. This configuration works with sql having uniqueidentifier as type on databse? My application with String as field and String getter/setter works fine with sql having field as uniqueidentifier, but for postgres it requires to have UUID type field in java for uuid in database. Is there any way to use String as field and uuid as database type for id in postgres.


Top
 Profile  
 
 Post subject: Re: pg-uuid with String getter and setter
PostPosted: Thu Sep 29, 2016 3:52 am 
Newbie

Joined: Thu Sep 29, 2016 1:11 am
Posts: 4
Also why does this configuration works with hibernate annotation as there is also the bean validation is being failed?


Top
 Profile  
 
 Post subject: Re: pg-uuid with String getter and setter
PostPosted: Thu Sep 29, 2016 5:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Only if you provide a custom type that builds on top of pg-uuid and uses String on the Java side instead of java.util.UUID. Check out this blog post for more details of how you should write such a custom type.


Top
 Profile  
 
 Post subject: Re: pg-uuid with String getter and setter
PostPosted: Thu Sep 29, 2016 5:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
There is a huge difference between how annotations and HBM are handled. As a rule of thumb, annotations are better supported than HBM. In future, we plan on extending the JPA orm.xml and maybe deprecate HBM.

For the moment, you can always mix HBM and annotations. Just make sure that during scanning, you process HBM first and annotations second:

Code:
<property name="hibernate.archive.autodetection" value="hbm, class" />


Top
 Profile  
 
 Post subject: Re: pg-uuid with String getter and setter
PostPosted: Thu Sep 29, 2016 5:56 am 
Newbie

Joined: Thu Sep 29, 2016 1:11 am
Posts: 4
Please share the link for the blog


Top
 Profile  
 
 Post subject: Re: pg-uuid with String getter and setter
PostPosted: Thu Sep 29, 2016 6:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Sure. I updated my other post to embed the link. Check it out. It's really easy to provide your own custom type.


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