You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by peter karich <pe...@yahoo.de> on 2006/08/03 17:25:47 UTC

cayenne INSERTs NULL values instead of strings

Hallo Cayenne Community,

I like your project! But I can't get a simple Example
to run. I used the newest hsqldb and the newest
cayenne.jar (I tried also your hsqldb-1.8.0.2.jar from
the modeler).

Now I do the following 
1. "Easy creating a SimplePerson object with the
modeler" (with name=VARCHAR and myint=INTEGER)
2. calling context.newObject
3. and commitChanges() 
The result of the last step is that cayenne queries an
INSERT with the correct Id 
and a NULL name?? I can't figure out whats wrong.

The map.xml:
<?xml version="1.0" encoding="utf-8"?>
<data-map project-version="1.1">
	<property name="defaultPackage" value="test"/>
	<property name="defaultSchema" value="PUBLIC"/>
	<db-entity name="SimplePerson" schema="PUBLIC">
		<db-attribute name="ID" type="INTEGER"
isPrimaryKey="true" isMandatory="true"/>
		<db-attribute name="myint" type="INTEGER"/>
		<db-attribute name="name" type="VARCHAR"
length="255"/>
	</db-entity>
	<obj-entity name="SimplePerson"
className="test.SimplePerson"
dbEntityName="SimplePerson">
		<obj-attribute name="myint" type="java.lang.Integer"
db-attribute-path="myint"/>
		<obj-attribute name="name" type="java.lang.String"
db-attribute-path="name"/>
	</obj-entity>
</data-map>


Here is the driver.xml:
<?xml version="1.0" encoding="utf-8"?>
<driver project-version="1.1"
class="org.hsqldb.jdbcDriver">
	<url value="jdbc:hsqldb:file:I:\secCayenne"/>
	<connectionPool min="1" max="1" />
	<login userName="sa"/>
</driver>


And thats the cayenne.xml
<?xml version="1.0" encoding="utf-8"?>
<domains project-version="1.1">
<domain name="SimpleTest">
	<map name="SimpleTestMap"
location="SimpleTestMap.map.xml"/>
	<node name="SimpleTestNode"
		 datasource="SimpleTestNode.driver.xml" 	
factory="org.objectstyle.cayenne.conf.DriverDataSourceFactory">
			<map-ref name="SimpleTestMap"/>
	 </node>
</domain>
</domains>



After creating the db schema and generating the
classes the program:

DataContext context = DataContext.createDataContext();
SimplePerson p =
(SimplePerson)context.newObject(SimplePerson.class);
p.setMyint(new Integer(22));
String lastName = "testName";
p.setName(lastName);
System.out.println(""+context.hasChanges());
context.commitChanges();

prints out the following:
INFO  QueryLogger: Created connection pool:
jdbc:hsqldb:file:I:\secCayenne
        Driver class: org.hsqldb.jdbcDriver
        Min. connections in the pool: 1
        Max. connections in the pool: 1
true
INFO  QueryLogger: Opening connection:
jdbc:hsqldb:file:I:\secCayenne
        Login: sa
        Password: *******
INFO  QueryLogger: +++ Connecting: SUCCESS.
INFO  QueryLogger: Detected and installed adapter:
org.objectstyle.cayenne.dba.hsqldb.HSQLDBAdapter
INFO  QueryLogger: --- will run 2 queries.
INFO  QueryLogger: --- transaction started.
INFO  QueryLogger: SELECT NEXT_ID FROM AUTO_PK_SUPPORT
WHERE TABLE_NAME = 'SimplePerson'
INFO  QueryLogger: === returned 1 row. - took 31 ms.
INFO  QueryLogger: UPDATE AUTO_PK_SUPPORT SET NEXT_ID
= NEXT_ID + 20 WHERE TABLE_NAME = 'SimplePerson'
INFO  QueryLogger: === updated 1 row.
INFO  QueryLogger: --- will run 1 query.
INFO  QueryLogger: INSERT INTO PUBLIC.SimplePerson
(ID, myint, name) VALUES (?, ?, ?)
INFO  QueryLogger: [bind: 240, NULL, NULL]         
<----- NULL, NULL is wrong!!??
INFO  QueryLogger: === updated 1 row.
...


Again: Where is my mistake?

Thank you, Peter Karich!




PS:
May be this helps you to understand what happend (I
debuggend through cayenne):
1. in line 120, class DataDomainInsertBucket, method
appendQueriesInternal():
after "if (isMasterDbEntity)
{createPermIdsForObjEntity(entity, objects); ..."
all is okay: objects contains one onbject:
SimplePersons(name="testName", myint=22)

2. BUT in DataDomainFlushAction.runQueries the method:
lastNode.performQueries(queries.subList(rangeStart,
len), observer);
will create a Collection named queries (size==1) in
DataNode and queryRunner.runQuery(connection,
nextQuery);

3. results: "INFO  QueryLogger: [bind: 240, NULL,
NULL]"



	

	
		
___________________________________________________________ 
Der frühe Vogel fängt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail: http://mail.yahoo.de

Re: cayenne INSERTs NULL values instead of strings

Posted by Mike Kienenberger <mk...@gmail.com>.
On 8/3/06, peter karich <pe...@yahoo.de> wrote:
> Yes the things are very basic and may be I am too
> stupid (hopefully not, so that I will not waste your
> time ...)
> Mike Kienenberger, I tried it with
> SimplePerson p =
> (SimplePerson)context.createAndRegisterNewObject(SimplePerson.class);
>
> and have the same problem...
>
> Now, I will start a new SimplePerson "project" from
> the scratch to figure out where the problem
> comes from. Which database do you use?

I'm currently using hsqldb 1.8.0.4 and Oracle 10.
I used hsqldb 1.8.0.2 and 1.7.3.3 previously without problem.

It's possible it's in the generated classes.   I have my own custom
generated classes which are pre-ObjectContext and are probably mostly
different.   The new classes look a lot different than what I'm used
to seeing.

Re: cayenne INSERTs NULL values instead of strings

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Aug 3, 2006, at 11:33 AM, Mike Kienenberger wrote:

> That's a bit odd.
>
> Have you tried using createAndRegisterNewObject() instead?
>
> newObject is part of the new ObjectContext api, and I haven't used  
> it myself.

'newObject' should work fine... This has to be something else.  
Strange indeed, as the entire thing is very basic.

Peter, could you post the source code for SimplePerson and  
_SimplePerson classes?

Andrus



Re: cayenne INSERTs NULL values instead of strings

Posted by Tore Halset <ha...@pvv.ntnu.no>.
On Aug 3, 2006, at 18:47, Mike Kienenberger wrote:

> I was thinking that maybe we'd somehow switched over to POJO  
> objects by default, and I'd missed the
> announcement.

Hehe :)

  - Tore.

Re: cayenne INSERTs NULL values instead of strings

Posted by Mike Kienenberger <mk...@gmail.com>.
On 8/3/06, Andrus Adamchik <an...@objectstyle.org> wrote:
> > public class _SimplePerson extends CayenneDataObject {
> >     public static final String MYINT_PROPERTY =
> > "myint";
> >     public static final String NAME_PROPERTY = "name";
> >     protected Integer myint;
> >     protected String name;
>
> This doesn't look right. I suggest that you delete all the generated
> classes, and in the class generator instead of "advanced" select
> "Standard Persistent Objects" and run the generator again.

Well, that makes more sense to me.  I was thinking that maybe we'd
somehow switched over to POJO objects by default, and I'd missed the
announcement.

Re: cayenne INSERTs NULL values instead of strings - SOLVED

Posted by Peter Karich <pe...@yahoo.de>.
n Thu, 03 Aug 2006 19:16:50 +0200, Joshua Pyle <jo...@gmail.com>  
wrote:

> What did ya have to do?


I did the following:

"I suggest that you delete all the generated classes, and in the class  
generator instead of "advanced" select "Standard Persistent Objects" and  
run the generator again."- Andrus

Peter

	

	
		
___________________________________________________________ 
Der fr�he Vogel f�ngt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail: http://mail.yahoo.de

Re: cayenne INSERTs NULL values instead of strings - SOLVED

Posted by Joshua Pyle <jo...@gmail.com>.
What did ya have to do?


-- 
Joshua T. Pyle
Go has always existed.


On 8/3/06, peter karich <pe...@yahoo.de> wrote:
> Thank you very much!
>
> My problem is solved! Now I get the none NULL values
> :-)
>
> (Are there any doc's available for the
> 'template-language'?)
>
> Peter.
>
>
>
> ___________________________________________________________
> Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de
>

Re: cayenne INSERTs NULL values instead of strings - SOLVED

Posted by Mike Kienenberger <mk...@gmail.com>.
For velocity itself, there's

http://jakarta.apache.org/velocity/docs/user-guide.html

If you're going to create your own customized templates, I'd recommend
using version 1.2 of cgen (the code generator).

This puts some code tools into your template context to let you do
common templating operations easier.

You can look at the default templates for examples, and taken from
there (for 1.2) are:

##  Classes available in template
##    objEntity - the ObjEntity class: See
org.objectstyle.cayenne.map.ObjectEntity
##    stringUtils - class for string "helper" functions: See
org.objectstyle.cayenne.gen.StringUtils
##    entityUtils - class for entity "helper" functions: See
org.objectstyle.cayenne.gen.EntityUtils
##    importUtils - class for import statement management: See
org.objectstyle.cayenne.gen.ImportUtils



On 8/3/06, peter karich <pe...@yahoo.de> wrote:
> Thank you very much!
>
> My problem is solved! Now I get the none NULL values
> :-)
>
> (Are there any doc's available for the
> 'template-language'?)
>
> Peter.
>
>
>
> ___________________________________________________________
> Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de
>

Re: cayenne INSERTs NULL values instead of strings - SOLVED

Posted by peter karich <pe...@yahoo.de>.
Thank you very much!

My problem is solved! Now I get the none NULL values
:-)

(Are there any doc's available for the
'template-language'?)

Peter.


		
___________________________________________________________ 
Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de

Re: cayenne INSERTs NULL values instead of strings

Posted by Andrus Adamchik <an...@objectstyle.org>.
On Aug 3, 2006, at 12:20 PM, peter karich wrote:

> public class _SimplePerson extends CayenneDataObject {
>     public static final String MYINT_PROPERTY =
> "myint";
>     public static final String NAME_PROPERTY = "name";
>     protected Integer myint;
>     protected String name;

This doesn't look right. I suggest that you delete all the generated  
classes, and in the class generator instead of "advanced" select  
"Standard Persistent Objects" and run the generator again.

Andrus


Re: cayenne INSERTs NULL values instead of strings

Posted by peter karich <pe...@yahoo.de>.
Thank you for your fast reply (wow!! :-)

Yes the things are very basic and may be I am too
stupid (hopefully not, so that I will not waste your
time ...)
Mike Kienenberger, I tried it with 
SimplePerson p =
(SimplePerson)context.createAndRegisterNewObject(SimplePerson.class);

and have the same problem...

Now, I will start a new SimplePerson "project" from
the scratch to figure out where the problem
comes from. Which database do you use?

Here is the generated source:
_SimplePerson.java
package test;
import org.objectstyle.cayenne.CayenneDataObject;
/**
 * A generated persistent class mapped as
"SimplePerson" Cayenne entity. It is a good idea to
 * avoid changing this class manually, since it will
be overwritten next time code is
 * regenerated. If you need to make any
customizations, put them in a subclass.
 */
public class _SimplePerson extends CayenneDataObject {
    public static final String MYINT_PROPERTY =
"myint";
    public static final String NAME_PROPERTY = "name";
    protected Integer myint;
    protected String name;

    public Integer getMyint() {
        if(objectContext != null) {
            objectContext.prepareForAccess(this,
"myint");
        }
        
        return myint;
    }
    public void setMyint(Integer myint) {
        if(objectContext != null) {
            objectContext.prepareForAccess(this,
"myint");
        }
        
        Object oldValue = this.myint;
        this.myint = myint;
        
        // notify objectContext about simple property
change
        if(objectContext != null) {
            objectContext.propertyChanged(this,
"myint", oldValue, myint);
        }
    }    
    
    public String getName() {
        if(objectContext != null) {
            objectContext.prepareForAccess(this,
"name");
        }
        
        return name;
    }
    public void setName(String name) {
        if(objectContext != null) {
            objectContext.prepareForAccess(this,
"name");
        }
        
        Object oldValue = this.name;
        this.name = name;
        
        // notify objectContext about simple property
change
        if(objectContext != null) {
            objectContext.propertyChanged(this,
"name", oldValue, name);
        }
    }
}

SimplePerson.java:
package test;
/**A persistent class mapped as "SimplePerson" Cayenne
entity. */
public class SimplePerson extends _SimplePerson {
}


Bye, Peter.





PS: Another problem occured if I choose "generator
version = 1.1" in the class generator 
dialog _SimplePerson.java will be:
${importUtils.setPackage($entityUtils.superPackageName)}${importUtils.addReservedType("${$entityUtils.superPackageName}.${entityUtils.superClassName}")}${importUtils.addType("${entityUtils.basePackageName}.${entityUtils.baseClassName}")}${importUtils.generate()}
/**
 * A generated persistent class mapped as
"${objEntity.name}" Cayenne entity. It is a good idea
to
 * avoid changing this class manually, since it will
be overwritten next time code is
 * regenerated. If you need to make any
customizations, put them in a subclass.
 */
public class ${entityUtils.superClassName} extends
${entityUtils.baseClassName} {
}


	

	
		
___________________________________________________________ 
Der frühe Vogel fängt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail: http://mail.yahoo.de

Re: cayenne INSERTs NULL values instead of strings

Posted by Mike Kienenberger <mk...@gmail.com>.
That's a bit odd.

Have you tried using createAndRegisterNewObject() instead?

newObject is part of the new ObjectContext api, and I haven't used it myself.

>From the api docs:
http://incubator.apache.org/cayenne/1_2/api/cayenne/org/objectstyle/cayenne/access/DataContext.html
====================
DataObjects are registered with DataContext either implicitly when
they are fetched via a query, or read via a relationship from another
object, or explicitly via calling
createAndRegisterNewObject(Class)during new DataObject creation.
DataContext tracks changes made to its DataObjects in memory, and
flushes them to the database when commitChanges()is called. Until
DataContext is committed, changes made to its objects are not visible
in other DataContexts.
====================


On 8/3/06, peter karich <pe...@yahoo.de> wrote:
> Hallo Cayenne Community,
>
> I like your project! But I can't get a simple Example
> to run. I used the newest hsqldb and the newest
> cayenne.jar (I tried also your hsqldb-1.8.0.2.jar from
> the modeler).
>
> Now I do the following
> 1. "Easy creating a SimplePerson object with the
> modeler" (with name=VARCHAR and myint=INTEGER)
> 2. calling context.newObject
> 3. and commitChanges()
> The result of the last step is that cayenne queries an
> INSERT with the correct Id
> and a NULL name?? I can't figure out whats wrong.
>
> The map.xml:
> <?xml version="1.0" encoding="utf-8"?>
> <data-map project-version="1.1">
>         <property name="defaultPackage" value="test"/>
>         <property name="defaultSchema" value="PUBLIC"/>
>         <db-entity name="SimplePerson" schema="PUBLIC">
>                 <db-attribute name="ID" type="INTEGER"
> isPrimaryKey="true" isMandatory="true"/>
>                 <db-attribute name="myint" type="INTEGER"/>
>                 <db-attribute name="name" type="VARCHAR"
> length="255"/>
>         </db-entity>
>         <obj-entity name="SimplePerson"
> className="test.SimplePerson"
> dbEntityName="SimplePerson">
>                 <obj-attribute name="myint" type="java.lang.Integer"
> db-attribute-path="myint"/>
>                 <obj-attribute name="name" type="java.lang.String"
> db-attribute-path="name"/>
>         </obj-entity>
> </data-map>
>
>
> Here is the driver.xml:
> <?xml version="1.0" encoding="utf-8"?>
> <driver project-version="1.1"
> class="org.hsqldb.jdbcDriver">
>         <url value="jdbc:hsqldb:file:I:\secCayenne"/>
>         <connectionPool min="1" max="1" />
>         <login userName="sa"/>
> </driver>
>
>
> And thats the cayenne.xml
> <?xml version="1.0" encoding="utf-8"?>
> <domains project-version="1.1">
> <domain name="SimpleTest">
>         <map name="SimpleTestMap"
> location="SimpleTestMap.map.xml"/>
>         <node name="SimpleTestNode"
>                  datasource="SimpleTestNode.driver.xml"
> factory="org.objectstyle.cayenne.conf.DriverDataSourceFactory">
>                         <map-ref name="SimpleTestMap"/>
>          </node>
> </domain>
> </domains>
>
>
>
> After creating the db schema and generating the
> classes the program:
>
> DataContext context = DataContext.createDataContext();
> SimplePerson p =
> (SimplePerson)context.newObject(SimplePerson.class);
> p.setMyint(new Integer(22));
> String lastName = "testName";
> p.setName(lastName);
> System.out.println(""+context.hasChanges());
> context.commitChanges();
>
> prints out the following:
> INFO  QueryLogger: Created connection pool:
> jdbc:hsqldb:file:I:\secCayenne
>         Driver class: org.hsqldb.jdbcDriver
>         Min. connections in the pool: 1
>         Max. connections in the pool: 1
> true
> INFO  QueryLogger: Opening connection:
> jdbc:hsqldb:file:I:\secCayenne
>         Login: sa
>         Password: *******
> INFO  QueryLogger: +++ Connecting: SUCCESS.
> INFO  QueryLogger: Detected and installed adapter:
> org.objectstyle.cayenne.dba.hsqldb.HSQLDBAdapter
> INFO  QueryLogger: --- will run 2 queries.
> INFO  QueryLogger: --- transaction started.
> INFO  QueryLogger: SELECT NEXT_ID FROM AUTO_PK_SUPPORT
> WHERE TABLE_NAME = 'SimplePerson'
> INFO  QueryLogger: === returned 1 row. - took 31 ms.
> INFO  QueryLogger: UPDATE AUTO_PK_SUPPORT SET NEXT_ID
> = NEXT_ID + 20 WHERE TABLE_NAME = 'SimplePerson'
> INFO  QueryLogger: === updated 1 row.
> INFO  QueryLogger: --- will run 1 query.
> INFO  QueryLogger: INSERT INTO PUBLIC.SimplePerson
> (ID, myint, name) VALUES (?, ?, ?)
> INFO  QueryLogger: [bind: 240, NULL, NULL]
> <----- NULL, NULL is wrong!!??
> INFO  QueryLogger: === updated 1 row.
> ...
>
>
> Again: Where is my mistake?
>
> Thank you, Peter Karich!
>
>
>
>
> PS:
> May be this helps you to understand what happend (I
> debuggend through cayenne):
> 1. in line 120, class DataDomainInsertBucket, method
> appendQueriesInternal():
> after "if (isMasterDbEntity)
> {createPermIdsForObjEntity(entity, objects); ..."
> all is okay: objects contains one onbject:
> SimplePersons(name="testName", myint=22)
>
> 2. BUT in DataDomainFlushAction.runQueries the method:
> lastNode.performQueries(queries.subList(rangeStart,
> len), observer);
> will create a Collection named queries (size==1) in
> DataNode and queryRunner.runQuery(connection,
> nextQuery);
>
> 3. results: "INFO  QueryLogger: [bind: 240, NULL,
> NULL]"
>
>
>
>
>
>
>
> ___________________________________________________________
> Der frühe Vogel fängt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail: http://mail.yahoo.de
>