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: SQL Server 2000 native id : Cannot insert the value NULL
PostPosted: Mon Apr 12, 2004 12:06 am 
Beginner
Beginner

Joined: Wed Feb 25, 2004 5:54 am
Posts: 30
I'm using Hibernate 2.1.2 with SQL Server 2000

Usually my code is running well in my machine, but when I tested to different SQL Server 2000 Agent, it throwed these exceptions :
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Cannot insert the value NULL into column 'ID', table 'ATC.dbo.CLIENTS'; column does not allow nulls. INSERT fails.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:508)



this is the mapping:

CLIENT.HBM.XML
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.andrewtani.tm.hbm.trax.Client" table="CLIENTS">
<id name="id" column="ID">
<generator class="native" />
</id>
<property name="name" column="CLIENT_CODE" not-null="true" />
<property name="desc" column="CLIENT_NAME" />

</class>

</hibernate-mapping>

This is the code I run when I get those exceptions : (in blue)

public class NewClient
{

private Client newClient;

public NewClient()
{
try {
newClient = createNew("RSO","Runnable System Operator"); } catch (HibernateException e) {

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

e.printStackTrace();
}
}


public Client createNew(String name, String desc) throws HibernateException
{
try
{
sess = sessFact.openSession();
tx = sess.beginTransaction();
newClient = new Client();
newClient.setName(name);
newClient.setDesc(desc);
sess.save(newClient);
tx.commit();
}
catch(HibernateException he)
{
if(tx!=null) tx.rollback();
throw he;
}
finally
{
sess.close();
}
return newClient;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 12, 2004 12:10 am 
Beginner
Beginner

Joined: Wed Feb 25, 2004 5:54 am
Posts: 30
sorry one more thing :
in hibernate.properties,
if I used this (i.e. TEST = my machine), the code would work
hibernate.connection.url=jdbc:microsoft:sqlserver://TEST:1433;DatabaseName=CLIENTDB;SelectMethod=cursor
hibernate.connection.driver_class=com.microsoft.jdbc.sqlserver.SQLServerDriver
hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.username=admin
hibernate.connection.password=password

however if I used this (i.e. ERE = SQL Server Agent in different machine), I won't work.
hibernate.connection.url=jdbc:microsoft:sqlserver://ERE:1433;DatabaseName=CLIENT2DB;SelectMethod=cursor
hibernate.connection.driver_class=com.microsoft.jdbc.sqlserver.SQLServerDriver
hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.username=sa
hibernate.connection.password=

Any bug in SQL Server JDBC Driver?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 6:55 am 
Beginner
Beginner

Joined: Wed Feb 25, 2004 5:54 am
Posts: 30
i'm still confused here. nothing said in the database. could anyone help me on this?

Should I change native to identity?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 6:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Does your db schema actually have an identity column?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 14, 2004 3:59 am 
Beginner
Beginner

Joined: Wed Feb 25, 2004 5:54 am
Posts: 30
yes, I do.

this is the generated SQL :

create table CLIENTS (
ID INT IDENTITY NOT NULL,
CLIENT_CODE VARCHAR(255) not null,
CLIENT_NAME VARCHAR(255) null,
primary key (ID)
)


and this is my Client.java :

public class Client
{
private Integer id;
private String clientCode;
private String clientName;


public String toString()
{
return "code = " +clientCode+ ", name = "+clientName;
}


public Integer getId() {
return id;
}


public String getClientName() {
return clientName;
}


public void setId(Integer theId) {
id = theId;
}



public String getClientCode() {
return clientCode;
}


public void setClientCode(String theCode) {
clientCode = theCode;
}

public void setClientName(String theName) {
clientName = theName;
}

}


Thanks for checkin it Gavin.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 14, 2004 2:04 pm 
Newbie

Joined: Sat Jan 03, 2004 11:35 am
Posts: 9
Location: Austria
try this one. it works well and my id column is exactly defined as yours.

Code:
<id unsaved-value="0" column="id" type="int" name="id">
  <generator class="identity"/>
</id>


guenz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 1:37 am 
Beginner
Beginner

Joined: Wed Feb 25, 2004 5:54 am
Posts: 30
guenz wrote:
try this one. it works well and my id column is exactly defined as yours.

Code:
<id unsaved-value="0" column="id" type="int" name="id">
  <generator class="identity"/>
</id>


guenz


I still get the same error message :

Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Cannot insert the value NULL into column 'ID', table 'ATC.dbo.CLIENTS'; column does not allow nulls. INSERT fails.

But the weird thing is I can always insert those stuffs in my own local database. Is there any settings in hibernate I need to know about for inserting to SQLServer2000 database on different machine?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 2:13 am 
Newbie

Joined: Sat Jan 03, 2004 11:35 am
Posts: 9
Location: Austria
sorry, was my mistake.

you can do the following:

either you map your id column as int (not as Integer) then my code snippet works. int works fine because your id column does not allow null that's why i do not see a problem.

or you change the unsaved-value attribute of the id tag to 'null' (if you use Integer for you id column) similar to this:
Code:
<id unsaved-value="null" column="id" type="int" name="id">
  <generator class="identity"/>
</id>


hope this helps.

guenz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 5:16 am 
Beginner
Beginner

Joined: Wed Feb 25, 2004 5:54 am
Posts: 30
whaow..

it works man
thanks..

but I wanna verify sumthin here..

the use of unsaved value: it is used for distinguishing new elements of collections with the old (saved) ones....am I right?

So is it a trivial thing or is it a must that in ID we put unsaved value in order to be able to save in other machine?

Thanks for answering, Hibernate God(s). :P


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.