-->
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.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: how to mention the autoincrement in composite-id
PostPosted: Tue Jan 03, 2006 12:54 am 
Newbie

Joined: Sun Jan 01, 2006 12:53 pm
Posts: 11
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

hi, i am using composite-id wich is having one auto-increment column. while saving i am not assigning the value for that column. and in the returned serialisable object giving null value for that column even though database insertion happend. i am getting problem while using this composite-id as foreign key. can anybody help me please..


Hibernate version:2.1

Mapping documents:
article.hbm.xml
<hibernate-mapping
>
<class
name="domain.Article"
table="t_article"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>

<composite-id name="articleId" class="domain.ArticleId">
<key-property name="articleId" column="article_id" type="java.lang.Long"/>
<key-property name="articleVersion" column="article_version" type="int"/>
</composite-id>
</class>

</hibernate-mapping>
article_content_block.hbm
<hibernate-mapping
>
<class
name="domain.ArticleContentBlockXRef"
table="t_xref_article_content_block"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>

<composite-id
name="articleContentBlockXRefId"
class="domain.ArticleContentBlockXRefId"
unsaved-value="any"
>

<key-property name="articleId"
column="article_id"
type="java.lang.Long"
access="field"
/>
<key-property name="articleVersion"
column="article_version"
type="int"
access="field"
/>

<key-many-to-one
name="contentBlock"
class="domain.ContentBlock"
column="content_block_id"
/>

</composite-id>

<many-to-one name="article" class="domain.Article" insert="false" update="false">
<column name="articleId"></column>
<column name="articleVersion"></column>
</many-to-one>
</class>

</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():articleid=sess.save(article); contentblock.setArticle(article);sess.save(contentblok);

Full stack trace of any exception that occurs:nullPointerException
[JDBCExceptionReporter] null, message from server: "Column 'article_id' cannot be null"
[SessionImpl] Could not synchronize database state with session
[HibernateUtil] Transaction Rolled Back


Name and version of the database you are using:mysql

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Last edited by kishore_jv on Sun Mar 12, 2006 6:27 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 3:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it does not make sense to have a unique id generated for composite-id's.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: please see this problem again... any body help me
PostPosted: Wed Jan 04, 2006 12:26 am 
Newbie

Joined: Sun Jan 01, 2006 12:53 pm
Posts: 11
I have a composite key where one column holds a generated value - how can I get Hibernate to generate and assign the value?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 1:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
write a UserType.

But you should really rethink why you have a composite key with one part being unique...what is the other part for ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: please see this problem again... any body help me
PostPosted: Wed Jan 04, 2006 6:30 am 
Newbie

Joined: Sun Jan 01, 2006 12:53 pm
Posts: 11
kishore_jv wrote:
I have a composite key where one column holds a generated value - how can I get Hibernate to generate and assign the value?



and that composite-id is used in an other composite id. while insertion there is no poroblem. while retriving it is giving exception like "Unknown column 'articleact0_.articleId' in 'field list'"



<hibernate-mapping
>
<class
name="domain.Article"
table="t_article"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>

<composite-id name="articleId" class="domain.ArticleId">
<key-property name="articleId" column="article_id" type="java.lang.Long"/>
<key-property name="articleVersion" column="article_version" type="int"/>
</composite-id>

</class>
</hibernate-mapping>


Last edited by kishore_jv on Sun Mar 12, 2006 6:23 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Max please give solution to this
PostPosted: Wed Jan 04, 2006 9:11 am 
Newbie

Joined: Sun Jan 01, 2006 12:53 pm
Posts: 11
max wrote:
write a UserType.

But you should really rethink why you have a composite key with one part being unique...what is the other part for ?



one is articleId and another is articleVersion. ArticleVersion may change or may not change.For each id there may be different versions.

This composite key values must be present where ever this id is reffered in other tables. So the other tables should have also impliment UserType.

Is there any other better way in Hibernate3.0. Please give me link or example for this.

CREATE TABLE `t_article` (
`article_id` int(30) unsigned NOT NULL auto_increment,
`article_version` int(10) unsigned NOT NULL default '1',
.....
.....
PRIMARY KEY (`article_id`,`article_version`),
...
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


Top
 Profile  
 
 Post subject: Max please give solution to this
PostPosted: Mon Feb 06, 2006 1:37 am 
Newbie

Joined: Sun Jan 01, 2006 12:53 pm
Posts: 11
kishore_jv wrote:
max wrote:
write a UserType.

But you should really rethink why you have a composite key with one part being unique...what is the other part for ?


Max as per u r question the one is articleId and another is articleVersion. ArticleVersion may change or may not change.For each id there may be different versions.

This composite key values must be present where ever this id is reffered in other tables. So the other tables should have also impliment UserType.

Is there any other better way in Hibernate3.0. Please give me link or example for this.

CREATE TABLE `t_article` (
`article_id` int(30) unsigned NOT NULL auto_increment,
`article_version` int(10) unsigned NOT NULL default '1',
.....
.....
PRIMARY KEY (`article_id`,`article_version`),
...
) ENGINE=MyISAM DEFAULT CHARSET=latin1;




Max can u find time to answer this question.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 2:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if there can be more than one table with the same article id then it is not an autoincrementing one!

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Max can u find time to check this
PostPosted: Mon Feb 06, 2006 3:05 am 
Newbie

Joined: Sun Jan 01, 2006 12:53 pm
Posts: 11
max wrote:
write a UserType.

But you should really rethink why you have a composite key with one part being unique...what is the other part for ?


For that problem i have writen a UserType and an IdentifierGenerater too. I have modified the IdentifierGeneratorFactory as follows...

public static Serializable get(ResultSet rs, Type type)
throws SQLException, IdentifierGenerationException {

Class clazz = type.getReturnedClass();
if ( clazz==Long.class ) {
return new Long( rs.getLong(1) );
}
else if ( clazz==Integer.class ) {
return new Integer( rs.getInt(1) );
}
else if ( clazz==Short.class ) {
return new Short( rs.getShort(1) );
}
else if ( clazz==String.class ) {
return rs.getString(1);
}
else if( clazz==domain.ArticleUserTypeId.class) {
Long articleId=(new Long(rs.getString(1)));
log.debug( "articleId ="+articleId);

return new domain.ArticleUserTypeId(articleId,0);
}
else {
throw new IdentifierGenerationException("this id generator generates long, integer, short or string");
}

}

And the i wrote Generator is as follows

public class CompositeIdGenerator extends SelectGenerator
{

/**
*
*/
public CompositeIdGenerator()
{
super();
// TODO Auto-generated constructor stub
}

private String uniqueKeyPropertyName;

private TextType idType;

private String entityName;

public void configure(TextType type, Properties params, Dialect d)
throws MappingException
{
uniqueKeyPropertyName = params.getProperty("key");
entityName = params.getProperty(ENTITY_NAME);
this.idType = type;
}

public Serializable generate(SessionImplementor session, Object obj)
throws HibernateException
{

final Serializable id = session.getEntityPersister(entityName, obj)
// TODO: cache the persister, this shows up in yourkit
.getIdentifier(obj, session.getEntityMode());

ArticleUserTypeId articleUserTypeId = (ArticleUserTypeId) id;
if (id == null)
{
throw new IdentifierGenerationException(
"ids for this class must be manually assigned before calling save(): "
+ entityName);
}
System.out
.println("In generate method of idnetity generator article id is "
+ articleUserTypeId.getArticleId());
System.out
.println("In generate method of idnetity generator article version is "
+ articleUserTypeId.getArticleVersion());

if (articleUserTypeId.getArticleId() == null)
{
return IdentifierGeneratorFactory.POST_INSERT_INDICATOR;
} else
{
return id;
}
}
}


Is this acceptable. If i modify like this i am able to insert the composite id with and without assigned values. But only one thing is to get clarification is .. Does this modification voilates the rules of hibernate architecture. If it does that please give show me a way to solve this problem with out modifing the hibernate files.

I will be waiting for your reply from you or from your team.
Max can u find some time to answer.
Anybody please help me in this problem.


Last edited by kishore_jv on Sun Mar 12, 2006 6:25 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: regarding the other tables
PostPosted: Mon Feb 06, 2006 3:26 am 
Newbie

Joined: Sun Jan 01, 2006 12:53 pm
Posts: 11
max wrote:
if there can be more than one table with the same article id then it is not an autoincrementing one!



Max thanks for your response

The following are the more details to this problem.

Max the other tables will have the coposite key with this article_id , article_version , and an other xref key. In that article id is not an autoincremeting one.

following is one the among those


article_operator CREATE TABLE `t_xref_article_operator` (
`operator_profile_id` int(30) NOT NULL default '0',
`operator_version` int(10) unsigned NOT NULL default '1',
`article_id` int(30) NOT NULL default '0',
`article_version` int(10) unsigned NOT NULL default '1',
PRIMARY KEY (`operator_profile_id`,`operator_version`,`article_id`,`article_version`)
)


In the insertion of rows for this table article id is assigned.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 4:08 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes - and ? you don't need/want automatic key generation, you want assigned.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 4:52 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It is a good idea to keep primary keys imutable and primary key is a minimal set of attributes to identify entity by definition (generated key must not be composite)
http://www.databasedesign-resource.com/ ... -keys.html


Top
 Profile  
 
 Post subject: All the remaining tables are assigned.
PostPosted: Mon Feb 06, 2006 6:25 am 
Newbie

Joined: Sun Jan 01, 2006 12:53 pm
Posts: 11
max wrote:
yes - and ? you don't need/want automatic key generation, you want assigned.


Max,

I need automatic key generation for article table only(column article Id is automatic generated and articleVersion is assigned).
But in the remaining tables(articleOperator) it is assigned.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 6:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
You wrote:

"ArticleVersion may change or may not change.For each id there may be different versions."

thus each id can be duplicated; thus it is not unique and thus it cannot be generated - capice ?

Again - auto generated values is not supported for composite id's because it does not make sense. period.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 6:54 am 
Newbie

Joined: Sun Jan 01, 2006 12:53 pm
Posts: 11
max wrote:
You wrote:

"ArticleVersion may change or may not change.For each id there may be different versions."

thus each id can be duplicated; thus it is not unique and thus it cannot be generated - capice ?

Again - auto generated values is not supported for composite id's because it does not make sense. period.



No there wont be any duplicates. Here is one example for that ...

If a new article is created we will use the article Id as automatic generated ..

ex: New Article
articleid articleVersion
1000 0


If anybody reviews on that article then the articleVersion will be incremeted by one. at that time articleId is assigned.

ex: After Review

articleId articleVersion
1000 1


In the creation of the article we dont assign the articleId (automatic increment). In the review that articleId is assigned. Here in this we dont get any duplicated rows.


Here is some example data from db

articleId articleVersion
1000 0
1000 1
1001 0
1002 0
1003 0
1003 1
1003 2
1003 3
1003 4
1004 0


Last edited by kishore_jv on Mon Feb 06, 2006 7:24 am, edited 1 time in total.

Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

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.