-->
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: Map Character to Boolean
PostPosted: Wed Jun 07, 2006 8:55 am 
Beginner
Beginner

Joined: Wed Jun 07, 2006 6:11 am
Posts: 20
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Name and version of the database you are using: Ingres

Hi!

I've got a table:

Code:
CREATE TABLE phone (
   id      INTEGER     NOT NULL,
   number  VARCHAR(20) NOT NULL,
   mobile  CHAR(1)     NOT NULL CHECK(mobile  = 'y' OR mobile  = 'n'),
   private CHAR(1)     NOT NULL CHECK(private = 'y' OR private = 'n'),
   fax     CHAR(1)     NOT NULL CHECK(fax     = 'y' OR fax     = 'n'),
   PRIMARY KEY(id)
);


From the hibernate tools I got this POJO:

Code:
@Entity
@Table(name = "phone", schema = "ingres", uniqueConstraints = {})
public class UsermgrPhone implements java.io.Serializable
{
    ...
  private Integer id;
  private String number;
  private Character mobile;
  private Character private_;
  private Character fax;
    ...
  @Column(name = "number", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
  public String getNumber()
  {
    return this.number;
  }


  @Column(name = "mobile", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
  public Character getMobile()
  {
    return this.mobile;
  }
    ...
}


Now, how can I tell Hibernate that this Character is a Boolean. Thus I'll get this POJO:

Code:
  @Column(name = "mobile", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
  public Boolean isMobile()
  {
    return ...;
  }


That means I want to map the Character to a Boolean. It doesn't matter how Hibernate will handle this case, but I want to use only the "Boolean" method, because I don't want to write:

Code:
if (getMobile() == 'y') ...


Thank you!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 1:04 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
A "CHAR" type does not map to a Java Boolean. A "BIT" type maps to a Boolean. What you are trying to do will not work with that table definition.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 1:04 pm 
Beginner
Beginner

Joined: Fri May 26, 2006 7:15 am
Posts: 20
Location: Vandavasi, TamilNadu
can u try formula tag ?

in xml
--------
<property name="mobile" type="java.lang.Boolean">
<formula>case when mobile = 'y' then 1 else 0 end</formula>
</property>

in java
--------
public Boolean isMobile()
{
return ...;
}




thanks

_________________
gajini
------------------


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 1:11 pm 
Regular
Regular

Joined: Mon May 22, 2006 2:30 pm
Posts: 74
If you write your own isMobile() method, then you don't even need the formula. It can check the value of the char. So your conditional test can be simply

Code:
if ( isMobile() ) {
   ...
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 1:33 pm 
Beginner
Beginner

Joined: Fri May 26, 2006 7:15 am
Posts: 20
Location: Vandavasi, TamilNadu
sorry,

In java
--------

public Boolean getMobile()
{
return this.mobile;
}

_________________
gajini
------------------


Top
 Profile  
 
 Post subject: @hibernate_user
PostPosted: Thu Jun 08, 2006 3:55 am 
Beginner
Beginner

Joined: Wed Jun 07, 2006 6:11 am
Posts: 20
Yeah, I know, but there is no type "BIT" in ingres, thus I used a CHAR type with 'y' and 'n'.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 4:54 am 
Beginner
Beginner

Joined: Wed Jun 07, 2006 6:11 am
Posts: 20
Got it now:

Code:
  @Column(name = "mobile", unique = false, nullable = false, insertable = true, updatable = true, length = 1)
  public Boolean getMobile()
  {
    return this.mobile == 'y';
  }
 
  public void setMobile(boolean mobile) {
    setMobile(mobile ? 'y' : 'n');
  }
 
  protected void setMobile(Character mobile)
  {
    this.mobile = mobile;
  }


Thanks guys


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 2:12 pm 
Beginner
Beginner

Joined: Fri May 26, 2006 7:15 am
Posts: 20
Location: Vandavasi, TamilNadu
Hi,

I think this will also be usefull to you.

yes_no: A type that maps an SQL CHAR(1) to a Java Boolean.

<property name="mobile" type="yes_no" />


thanks

_________________
gajini
------------------


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.