-->
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.  [ 10 posts ] 
Author Message
 Post subject: default value for premitives
PostPosted: Mon Nov 07, 2005 7:40 pm 
Newbie

Joined: Fri Nov 04, 2005 3:04 pm
Posts: 15
I understand that if any column is nullable then we should have no-primitives mapped.
But can i have a primitive set to some default value?

Thanks,
mp


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 9:05 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Yes. In your java class, set it up in your constructor or at declaration time. It's not an Hibernate issue.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 9:08 pm 
Newbie

Joined: Fri Nov 04, 2005 3:04 pm
Posts: 15
Thanks for the reply, is there any way i can handle this at hibernate level?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 9:16 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Hibernate doesn't care about values, just mappings. If you really, really want the hibernate mapping to handle it, then you can use the formula or sql-insert tags, as appropriate. Both of these will do it, but only when you save the object to the DB, not when you create the object in java. You realy should use declaration-time initialization, that's what it's for.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 5:07 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Implement custom types http://www.hibernate.org/hib_docs/v3/re ... pes-custom (see "DefaultValueIntegerType" example)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 16, 2007 8:44 am 
Newbie

Joined: Wed Feb 14, 2007 12:56 pm
Posts: 4
tenwit wrote:
Yes. In your java class, set it up in your constructor or at declaration time. It's not an Hibernate issue.


Could you pass me a link with documentation / tutorial about this?

Thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 4:31 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I just meant that you can do "int myPrimitive = 3;". But you already knew that.

If you don't want to set up a custom type, then you can "cheat" by using two accessor/mutator pairs. Something like this:
Code:
Integer realValue = null;

/** This one is called by API users */
public int getValue()
{
  if (realValue == null)
  {
    return 3;
  }
  return realValue;
}
public void setValue(int value)
{
  realValue = value;
}
public void unsetValue()
{
  realValue = null;
}
public Integer getRealValue()
{
  return realValue();
}
public void setRealValue(Integer value)
{
  realValue = value;
}
If you use realValue as the property in your mapping file, but put only getValue/setValue in your public interface, then you effectively have a default mapping for a primitive, mapped using standard java wrapper classes.

unsetValue() is the API call to null out the fake-primitive column. It might not be needed.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 26, 2007 11:38 am 
Newbie

Joined: Sun Feb 25, 2007 1:35 pm
Posts: 5
tenwit wrote:
I just meant that you can do "int myPrimitive = 3;". But you already knew that...

Hi there,

I'm trying to set up an index over 2 Strings and 1 Integer via the following code section:
Code:
@Entity
@Embeddable
public class Person {
   private Long id;
   private int age;
   private String firstname;
   private String lastname;

   @GeneratedValue
   public Long getId() {
      return id;
   }
   
   @SuppressWarnings("unused")
   private void setId(Long id) {
      this.id = id;
   }

   @Id
   public int getAge() {
      return age;
   }
   
   public void setAge(int age) {
      this.age = age;
   }

   @Column(length=30)
   @Id
   public String getFirstname() {
      return firstname;
   }
   
   public void setFirstname(String firstname) {
      this.firstname = firstname;
   }

   @Id
   public String getLastname() {
      return lastname;
   }
   
   public void setLastname(String lastname) {
      this.lastname = lastname;
   }

   public Person() {
      firstname = "foo";
      lastname = "bar";
      age = 0;
   }
   
   public Person(String firstName, String lastName, int age) {
      this();
      this.firstname = firstName;
      this.lastname = lastName;
      this.age = age;
   }

   @Override
   public boolean equals(Object object) {
      if (object instanceof Person) {
         Person otherPerson = (Person) object;
         
         return
            firstname.equals(otherPerson.getFirstname()) &&
            lastname.equals(otherPerson.getLastname()) &&
            (age == otherPerson.getAge());
      }
      return false;
   }

   @Override
   public int hashCode() {
      return (firstname + lastname + age).hashCode();
   }
}


As you can see there are default values set in the empty constructor and the empty constructor is called from each other contructor so any variable can't be uninitialized.
Even thought I should have set a default value for each of the index variables I get the following Exception thrown when inserting an object of this class into my database:

Code:
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at events.EventManager.main(EventManager.java:40)
Caused by: java.sql.BatchUpdateException: Field 'lastname' doesn't have a default value
   at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
   ... 8 more


Does anyone have an idea of how to solve this issue as it should be somehow related to hibernate not "recognizing" the default value for lastname.
Any help would be appreciated.
Thanks in advance,

Marc


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 26, 2007 4:30 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Looks to me like you're doing something like "new Person(null, null, 0);". That would mean that the default value of lastname's column would be required. Your code isn't guaranteeing that all values are initialized. You need to set all the values that the user passes in, then give uninitialized variables their default values.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 26, 2007 8:46 pm 
Newbie

Joined: Sun Feb 25, 2007 1:35 pm
Posts: 5
I instantiated the person with "real" values.
I'm not shure what the actual problem was but using an IdClass solved the problem for me. Thank for your help anyway.


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