-->
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.  [ 9 posts ] 
Author Message
 Post subject: Mapping should take over the ID from Database
PostPosted: Thu Dec 15, 2005 11:27 am 
Newbie

Joined: Wed Sep 28, 2005 5:05 am
Posts: 11
Can anybody help me?
I´m using Microsoft SQL 2005 and in the Dataschema the id is generated by mssql.
Now i wanna map this database to Java. For that i´m using Hibernate 2.1.8.
It seemed its necessary to map also the ID with ID-Tag and a generator class="identity"...now my question:
How can i tell the map-file that it shouldn´t use the id-generator but the generated id from database?

Or is there a special way to map it?

I found nothing about this problem since days. I can´t believe there is no one other person who works with a given database wich generate its own id and would now map it with hibernate :)

THX for help!!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 11:40 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
there is a native option for the generator tag

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 12:13 pm 
Newbie

Joined: Wed Sep 28, 2005 5:05 am
Posts: 11
kochcp wrote:
there is a native option for the generator tag


Yes, i know ;) i tried this...and all the other "possible" options...Let me show you my id-tag:

<id
name="Id"
column="__Org_RecordNo"
type="long"
unsaved-value="null">
<generator class="native"/>

</id>

the id-column (__Org_Re....) is sql-type integer..and in Microsoft SQL there is a option for primary key called :"is identity - yes/no" and the setting is "yes".. and it is not allowed to me to change it into "no" in coz of other applications...
But for testing i changed one time to "no"...and i was able to record data´s. But in given situation and my actuall mapping i always get following exception:

net.sf.hibernate.exception.GenericJDBCException: could not insert: [de.consistec.Ansprech_Partner.Adressen]
at net.sf.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:80)
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1331)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:540)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:426)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:28)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:943)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:868)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:786)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:749)
at de.consistec.Ansprech_Partner.InsertTests.NeueAdressen(InsertTests.java:54)
at de.consistec.Ansprech_Partner.InsertTests.main(InsertTests.java:101)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The statement must be executed before the generated keys are available
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getGeneratedKeys(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sf.hibernate.util.GetGeneratedKeysHelper.getGeneratedKey(GetGeneratedKeysHelper.java:59)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:520)
... 9 more


also when i try

<generator class="identity"/> (should normaly work with mssql)
i get the same message as above...

THX ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 12:52 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
I'm a Hibernate 3.x user, but I've had no problems with identity columns and SQLServer (2000, though, not 2005). I've also never had to specify an unsaved-value. I wonder if that is messing things up. Did you try removing that?

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 12:52 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
hrmmm

i have
Code:
    <id
        name="intSeqNo"
        type="java.lang.Integer"
        column="int_seq_no"
    >
        <generator class="native">
            <param name="sequence">jobqd_seq_no</param>
        </generator>
    </id>

working fantastically in mssql.

I've never seen that microsoft sql errror, the one about " The statement must be executed before the generated keys are available"

you're not using hte microsoft jdbc drivers are you?

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 12:53 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
the sequence tag is so that it runs in oracle as well

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 2:24 pm 
Newbie

Joined: Wed Sep 28, 2005 5:05 am
Posts: 11
nathanmoon wrote:
I'm a Hibernate 3.x user, but I've had no problems with identity columns and SQLServer (2000, though, not 2005). I've also never had to specify an unsaved-value. I wonder if that is messing things up. Did you try removing that?


Yes..i also removed that part...no effect.. Did you overtook the generated key from database in your part, and if, could you show me the code of the mapping? I´m not at work in moment..so tomorrow i can go on with searching

I´m very thankfull for every idea...so far ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 3:17 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Not sure what you mean. The Hibernate IdentifierGenerator for identity columns (which is what is used with "native" on SQLServer) uses the SQLServer identity column functionality. There isn't anything that you need to override or disable. It should work fine. My mappings look just like the one posted by kochcp, nothing fancy.

I would try out kochcp's suggestion of trying a different JDBC driver.

Good luck.

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2005 8:52 am 
Newbie

Joined: Wed Sep 28, 2005 5:05 am
Posts: 11
Ok..for the moment i found a answer...not very clean...But for the moment it´s ok..
- i recorded a data by creating a sql-statement without hibernate..just with native-sql by java.
- i searched the data with given dummy-name and load this object...
at last i made a update from values of columns by hibernate-language.

Now the code looks like this:

public void insertDummyAdresse()
{
System.out.println("Dummydatensatz wird angelegt");
System.out.println();

String sqlq="insert into Adressen (Name) values('"+dName+"')";


//Manueller Verbindungsaufbau ohne Hibernate
Connection cn = null;
Statement st = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
cn = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=OfficeSQL","sa","1100110");
st = cn.createStatement();
st.executeUpdate(sqlq);

st.close();
cn.close();
System.out.println("Angelegt");

}
catch (Exception ex)
{
System.out.println(ex);
}
}
public void updateAdresse()
{
insertDummyAdresse();
try
{
//Verbindung aufbauen
Session session=HibernateUtil.currentSession();
Transaction trx = session.beginTransaction();
//Lade inserDummyAdressen-Datensatz
List dDs =session.find("from Adressen where Name='"+dName+"'");
for (int i=0; i< dDs.size(); i++)
{
Adressen neueAdresse = (Adressen)dDs.get(i);
System.out.println("Name: "+neueAdresse.getName());
System.out.println("ID : "+neueAdresse.getId());
dId=(int) neueAdresse.getId();
}

//Update
Adressen uAdresse=(Adressen) session.load(Adressen.class,dId);
String eingabe;
eingabe=readEntry("Bitte geben Sie den Namen an : ");
uAdresse.setName(eingabe);
//session.update(uAdresse);
session.flush();
trx.commit();
HibernateUtil.closeSession();
}

catch (HibernateException e)
{
e.printStackTrace();
}

}


Now i can go one with my primary work...but i will fix it as it should be..so..if anybody have an idea...i´ll check this posting more times a week ;)

THX alote for helping....


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