You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by Nick Pomfret <ni...@pixelpark.com> on 2001/10/01 18:09:20 UTC

advice on inserts sought

I need to insert a row in table A who's primary key is a foreign key to
another table (B).  I have to insert the row in tableb B first, but where in
my code is the best place to do this?

What I've implemented is adding a B-bean with a 'get' and 'set' method to my
A-bean.  I've overridden the 'doInsert(obj, dbConn)' method in the A-peer
which now first gets the B-bean from the A-bean and calls doInsert on the
B-bean.  Then the A-bean calls doInsert on itself.  Here is the code:

	public static void doInsert( A obj, DBConnection dbCon) throws Exception
	{
		// first save the B (in order to get its primary key for this object)
		B bBean = obj.getB();
		BPeer.doInsert(bBean, dbCon);

		// set the primary key for the new A
		obj.setPrimaryKey(bBean.getPrimaryKey());

		// now insert this object
		Criteria criteria = buildCriteria(obj);

        doInsert(buildCriteria(obj), dbCon);
        obj.setNew(false);
	}

Is this the 'normal' way of doing this type of transaction or am I barking
up the wrong tree?

Nick


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org


Re: FileItem and character encoding problem and solution

Posted by Daniel Rall <dl...@finemaltcoding.com>.
"Nick Pomfret" <ni...@pixelpark.com> writes:

> My solution to was to alter the code in the method FileItem.write so that it
> uses a FileOutputStream instead of a FileWriter, this allows me to write the
> byte[] content directly into the file rather than converting it into a
> String object first.  The write method now looks like this:
...
> I'd be interested to hear of anyone else who has encountered this problem
> and may have come up with a less drastic solution.

Hi Nick.  There've been reports of a lot of trouble with uploads,
actually.  Your solution doesn't sound so bad to me.  Anyone else care
to comment on this?

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


FileItem and character encoding problem and solution

Posted by Nick Pomfret <ni...@pixelpark.com>.
We have TDK2.1 running on Redhat Linux 7.1

We were finding that characters such as u and e being submitted from HTML
forms were getting mangled into other characters.  We tested the software on
a win2k machine and the problem didn't occur.

After some digging we discovered that the set-up up of the Linux machine was
incorrect.  Setting the LC_ALL environment variable to en_US.utf8 and adding
enctype="multipart/form-data" to our <FORM> tags seemed to solve our problem
only to create another:

Now when we submitted an image file (a jpeg) from a form (which used to
work) the org.apache.turbine.util.upload.FileItem class always writes a file
of size 0 bytes .  I've tracked this problem down to the
FileItem.getString() method which returns String of length 0, I don't know
why though - any ideas?

The latest source from CVS (Revision 1.3 ) didn't solve the problem so...

My solution to was to alter the code in the method FileItem.write so that it
uses a FileOutputStream instead of a FileWriter, this allows me to write the
byte[] content directly into the file rather than converting it into a
String object first.  The write method now looks like this:

public void write(String file) throws Exception
{
	if (inMemory())
	{
		/* this doesn't seem to work
		FileWriter writer = null;
		*/
		FileOutputStream fout = null;
		try
		{
			fout = new FileOutputStream(file);
			fout.write(get());

			/* this doesn't seem to work
			writer = new FileWriter(file);
			writer.write(getString());
			*/
		}
		finally
		{
			/* this doesn't seem to work
			if (writer != null)
			{
				writer.close();
			}
			*/
			if (fout != null) {
				fout.flush();
				fout.close();
			}
		}
	}
	else if (storeLocation != null)
	{
		/*
		 * The uploaded file is being stored on disk
		 * in a temporary location so move it to the
		 * desired file.
		 */
		if (storeLocation.renameTo(new File(file)) == false)
		{
			throw new Exception(
				"Cannot write uploaded file to disk!");
		}
	}
	else
	{
		/*
		 * For whatever reason we cannot write the
		 * file to disk.
		 */
		throw new Exception("Cannot write uploaded file to disk!");
	}
}



I'd be interested to hear of anyone else who has encountered this problem
and may have come up with a less drastic solution.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


possible bug in torque generated code

Posted by Nick Pomfret <ni...@pixelpark.com>.
The copy() method in the torque generated in the object model doesn't seem
to work as I would expect.

When using the copy() method on an object it correctly copies that object
but not those objects dependent upon it.  Looking at the automatically
generated code it would appear that copying dependent objects is what is
supposed to happen (see torque-generated code below for the class 'Foo',
which contains other objects 'Bar') - am I correct?

public Foo copy() throws Exception
{
  Foo copyObj = new Foo();
  copyObj.setFooId(foo_id);

  v = copyObj.getBars();
  for (int i=0; i<v.size(); i++)
  {
    ((Persistent)v.get(i)).setNew(true);
  }

  copyObj.setFooId((NumberKey)null);
  return copyObj;
}

The copyObj is created at the start of this method and so never has any
'Bars', why then call 'getBars()' as the result will always be an empty
vector?

Assuming I've not got the wrong end of the stick completely, would the
following be an adequate solution?

public Foo copy() throws Exception
{
  Foo copyObj = new Foo();
  copyObj.setFooId(foo_id);

  v = getBars();
  for (int i=0; i<v.size(); i++)
  {
    Bar obj = (Bar) v.get(i);
    copyObj.addBar(obj.copy());
    ((Persistent)v.get(i)).setNew(true);
  }

  copyObj.setFooId((NumberKey)null);
  return copyObj;
}

If so, Object.vm (in torque) would need to be modified so that the code
which generates the changes described is:

  ${list}v = get${pCollName}();
  for (int i=0; i<v.size(); i++)
  {
    $className obj = ($className) v.get(i);
    copyObj.add$className(obj.copy())
    ((Persistent)v.get(i)).setNew(true);
  }

I'm new to this stuff so forgive me if I've miss understood!


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: varbinary columns in postgresql

Posted by Santiago Gala <sg...@hisitech.com>.
Nick Pomfret wrote:

>I've posted this before but didn't manage to get it cleared up.
>
>After installing the sample app that comes with TDK, and modifying the
>postgresql driver to work with oid fields, i get the following error in
>turbine.log after adding a new user:
>
>[Mon Oct 08 09:37:52 BST 2001] -- ERROR -- Error rendering Velocity
>template: sc
>reens/user/FluxUserList.vm: Error rendering Velocity template:
>screens/user/Flux
>UserList.vm: Invocation of method 'getUsers' in  class
>org.apache.turbine.flux.t
>ools.FluxTool threw exception class
>org.apache.turbine.util.security.DataBackend
>Exception
>
>A row was entered in the database and the other forms (such as the groups
>form) appear to be working correctly.
>
I think the problem is that village writes varbinary objects using 
connection.setBytes(), and this will not work with portgres cvs drivers 
for oid fields (patched or not), at least until 7.2 is out and we get 
better bytea support. If village used connection.setObject() it *could* 
work with patches applied to the jdbc driver.

In Jetspeed we are using a text DB field (instead of oid) for postgres 
varbinary objects, as this data type can handle byte arrays, which the 
postgres jdbc driver sends always as quoted strings to the backend.

The JDBC driver in postgres is a real mess, as it is changing very fast. 
I think the patch as marked in turbine web page is not valid anymore. So 
take my comments with a grain of salt :-)

>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: turbine-user-help@jakarta.apache.org
>




---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org


varbinary columns in postgresql

Posted by Nick Pomfret <ni...@pixelpark.com>.
I've posted this before but didn't manage to get it cleared up.

After installing the sample app that comes with TDK, and modifying the
postgresql driver to work with oid fields, i get the following error in
turbine.log after adding a new user:

[Mon Oct 08 09:37:52 BST 2001] -- ERROR -- Error rendering Velocity
template: sc
reens/user/FluxUserList.vm: Error rendering Velocity template:
screens/user/Flux
UserList.vm: Invocation of method 'getUsers' in  class
org.apache.turbine.flux.t
ools.FluxTool threw exception class
org.apache.turbine.util.security.DataBackend
Exception

A row was entered in the database and the other forms (such as the groups
form) appear to be working correctly.


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org


Re: advice on inserts sought

Posted by John McNally <jm...@collab.net>.
There is code generated that can help with this.  You can write

B b = a.getB();

// make sure to include A in the save
b.addA(a);
b.save();

This will have pretty much the same effect as the code below.  Look at
the code in the BaseB and BaseA classes to see how it compares with your
code.

john mcnally

Nick Pomfret wrote:
> 
> I need to insert a row in table A who's primary key is a foreign key to
> another table (B).  I have to insert the row in tableb B first, but where in
> my code is the best place to do this?
> 
> What I've implemented is adding a B-bean with a 'get' and 'set' method to my
> A-bean.  I've overridden the 'doInsert(obj, dbConn)' method in the A-peer
> which now first gets the B-bean from the A-bean and calls doInsert on the
> B-bean.  Then the A-bean calls doInsert on itself.  Here is the code:
> 
>         public static void doInsert( A obj, DBConnection dbCon) throws Exception
>         {
>                 // first save the B (in order to get its primary key for this object)
>                 B bBean = obj.getB();
>                 BPeer.doInsert(bBean, dbCon);
> 
>                 // set the primary key for the new A
>                 obj.setPrimaryKey(bBean.getPrimaryKey());
> 
>                 // now insert this object
>                 Criteria criteria = buildCriteria(obj);
> 
>         doInsert(buildCriteria(obj), dbCon);
>         obj.setNew(false);
>         }
> 
> Is this the 'normal' way of doing this type of transaction or am I barking
> up the wrong tree?
> 
> Nick
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: turbine-user-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org