Replacing empty string with empty_clob()

23 views
Skip to first unread message

cremor

unread,
Oct 1, 2010, 8:53:13 AM10/1/10
to nhusers
Is there a way to tell NHibernate to replace all values of empty
strings with the Oracle function empty_clob() in inserts/updates to a
specific column?

I'd need this to insert a empty string to a CLOB column which has a
NOT NULL constraint (Oracle treats empty strings the same as NULLs, so
it tries to insert a NULL to my CLOB column).

Fabio Maulo

unread,
Oct 1, 2010, 8:58:26 AM10/1/10
to nhu...@googlegroups.com
After register the empty_clob() function in your own dialect, you can try using the "default" attribute of <column>.


--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

cremor

unread,
Oct 4, 2010, 2:55:37 AM10/4/10
to nhusers
Thanks for your fast response, but I can't get this to work.

My Dialect (the method is called during startup, so it should be
configured correctly):
public class ExtendedOracleDialect : Oracle10gDialect {
protected override void RegisterFunctions() {
base.RegisterFunctions();
RegisterFunction("empty_clob", new
NoArgSQLFunction("empty_clob", NHibernateUtil.String, true));
}
}

My mapping (inside a double nested-composite-element, but I hope this
doesn't change anything):
<property name="Formatted" type="System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ERSETZUNG_FORMATIERT"
default="empty_clob()" />
</property>

But the generated insert statements are still using '' for
String.Empty and NULL for null instead of empty_clob(), which I need
in both cases.

On 1 Okt., 14:58, Fabio Maulo <fabioma...@gmail.com> wrote:
> After register the empty_clob() function in your own dialect, you can try
> using the "default" attribute of <column>.
>
>
>
> On Fri, Oct 1, 2010 at 9:53 AM, cremor <cre...@gmx.net> wrote:
> > Is there a way to tell NHibernate to replace all values of empty
> > strings with the Oracle function empty_clob() in inserts/updates to a
> > specific column?
>
> > I'd need this to insert a empty string to a CLOB column which has a
> > NOT NULL constraint (Oracle treats empty strings the same as NULLs, so
> > it tries to insert a NULL to my CLOB column).
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> > .

Fabio Maulo

unread,
Oct 4, 2010, 7:46:33 AM10/4/10
to nhu...@googlegroups.com
1. el type de la property es incorrecto
2. lo que te pase es porque no está generando la base con NH y/o sobre todo no estas comparando lo que NH piensa que está con lo que vos pusiste.

To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

Fabio Maulo

unread,
Oct 4, 2010, 9:10:11 AM10/4/10
to nhu...@googlegroups.com
Ups sorry.... 3 chats-mails-language-mixing
1. the type of the property is wrong
2. you are not generating the DB through NH and/or, over all, you are not comparing what NH thinks you have in schema with what you already have
--
Fabio Maulo

cremor

unread,
Oct 5, 2010, 2:32:51 AM10/5/10
to nhusers
No, I'm not generating the DB with NHibernate. The DB already exists
and the changes I can make to it are limited. Adding a default value
to the column in the DB is not possible.

But I found a solution. I implemented an IUserType with the following
code:

public void NullSafeSet(IDbCommand cmd, object value, int index)
{
string sValue = value as string;

if (sValue == String.Empty) {
OracleCommand oCmd = cmd as OracleCommand;

if (oCmd != null) {
oCmd.Parameters[index].OracleDbType =
OracleDbType.Clob;
oCmd.Parameters[index].Value = String.Empty;
}
} else {
NHibernateUtil.String.NullSafeSet(cmd, value, index);
> >> <nhusers%2Bunsu...@googlegroups.com<nhusers%252Buns...@googlegroups.com>
>
> >> > > .
> >> > > For more options, visit this group at
> >> > >http://groups.google.com/group/nhusers?hl=en.
>
> >> > --
> >> > Fabio Maulo
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "nhusers" group.
> >> To post to this group, send email to nhu...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/nhusers?hl=en.
>
> > --
> > Fabio Maulo
>
> --
> Fabio Maulo

Fabio Maulo

unread,
Oct 5, 2010, 7:36:07 AM10/5/10
to nhu...@googlegroups.com
If you have existing mappings using the StringClob type (or a certain
type), you can use a custom Driver to do the same work without touch
existing mappings.

--
Fabio Maulo

> To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages