-->
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.  [ 3 posts ] 
Author Message
 Post subject: can not make a many-to-one relationship optional
PostPosted: Mon Jul 27, 2009 5:51 am 
Newbie

Joined: Mon Jul 27, 2009 5:24 am
Posts: 3
Hello,
I have a Many-to-one relationship between Token and TokenTrack classes.
The Token primary key consistes of three columns (composite key).
I need to make this relationship optional=false but couldn't do that.

Code:
    @ManyToOne(optional = false)
    @ForeignKey(name = "token_track_fk_site_date_number")
    @JoinColumns({
        @JoinColumn(name = "site_code1", columnDefinition="NUMBER(3)", referencedColumnName="site_code" ),
        @JoinColumn(name = "token_date1", columnDefinition = "Date", referencedColumnName="token_date"),
        @JoinColumn(name = "token_number1", columnDefinition = "NUMBER(10)", referencedColumnName="token_number")
    })
    public Token getToken()
    {
        return _token;
    }


The error I get is


Code:
SEVERE: not-null property references a null or transient value: com.smartcube.queue.domain.TokenTrack.token
org.hibernate.PropertyValueException: not-null property references a null or transient value: com.smartcube.queue.domain.TokenTrack.token
   at org.hibernate.engine.Nullability.checkNullability(Nullability.java:95)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:313)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
   at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:534)


even though the token is not null and it's persisted in the DB and it is already assoiated with the tokenTrack object that I need to persist.

when I make the reationship optional = true and update the DB table the code works as required, but I need to prevent null values in forign key columns, so the optional = true is not an option.

best wishes


Top
 Profile  
 
 Post subject: Re: can not make a many-to-one relationship optional
PostPosted: Tue Jul 28, 2009 4:49 am 
Newbie

Joined: Mon Jul 27, 2009 5:24 am
Posts: 3
I found the root cause of the problem.
the following method is executed when the hibernate persist TokenTrack object, inside new ForeignKeys.Nullifier() method some code try to look for the token object in the DB, but it couldn't find the token, when this happen it nullify the token so this explain the error message.
Code:
      
   protected Serializable performSaveOrReplicate(
         Object entity,
         EntityKey key,
         EntityPersister persister,
         boolean useIdentityColumn,
         Object anything,
         EventSource source,
         boolean requiresImmediateIdAccess) {
...
new ForeignKeys.Nullifier( entity, false, useIdentityColumn, source )
            .nullifyTransientReferences( values, types );
...


But why hibernate can't find the token object even it's persisted in the DB!!!!!
I tried to find the token by ID and i couldn't
Code:
Token myToken = _tokenDAO.findById(token.getTokenPK());

the TokenPK is a composit primary key that consist of three columns (integer, integer, date)
I think that the problem is related to date column, any help please advice.

BTW: I know that composite keys are bad practice and we should prefer surrogate keys, but I tried and failed to change the system analyst mentality who do this design.


Top
 Profile  
 
 Post subject: Re: can not make a many-to-one relationship optional
PostPosted: Tue Jul 28, 2009 8:16 am 
Newbie

Joined: Mon Jul 27, 2009 5:24 am
Posts: 3
Finally I found the solution, As said previosly the problem may be related to the date column and it's so. I changed the Date column type in the Oracle DB from "Date" to "Timestamp"


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