You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Christian Defoy <ch...@gmail.com> on 2007/08/30 17:16:49 UTC

Skip a computed field on insert and update

Hello,

I have a Shape object that is referred to by an Attachment object.
The ID for Attachment is computed based on the ID of the related Shape
and the database enforces that by having the Attachment.shape_id field
as a computed field.

create table Shape
(
    shape_id int identity not null primary key
);

create table Attachment
(
    attachment_id int not null primary key,
    shape_id as (attachment_id / 65536),
    name varchar(20)
);

(Note: the DB is SQL Server)

The Attachment object has a reference to the Shape object but I must
prevent OpenJPA from trying to persist that reference to the database.
 However, OpenJPA must be able to load the Shape when I load the
Attachment. Is that possible?  If so, how do I do that?

I use the following annotations in Attachment to refer to Shape, but
that only gets me an exception.

    @ManyToOne( fetch=FetchType.LAZY )
    @Column( name="shape_id" )
    private Shape shape;

The exception is:

org.apache.openjpa.lib.jdbc.ReportingSQLException: Column 'shape_id'
cannot be modified because it is a computed column. {prepstmnt
30638546 INSERT INTO attachment (attachment_id, name, shape_id) VALUES
(?, ?, ?) [params=(int) 1900544, (String) MyAttach, (int) 29]}
[code=271, state=S0001]

Thanks in advance!

Christian

Re: Skip a computed field on insert and update

Posted by Christian Defoy <ch...@gmail.com>.
Patrick,

Yep, that's what I want to do.  Thanks!

Christian

On 8/30/07, Patrick Linskey <pl...@gmail.com> wrote:
> Hi,
>
> I think that you want to do the following:
>
> >     @ManyToOne( fetch=FetchType.LAZY )
> >     @Column( name="shape_id" )
> >     private Shape shape;
>
> @Column( name="shape_id", insertable=false, updatable=false )
>
> That will tell OpenJPA to not insert or update those columns.
>
> -Patrick
>
> On 8/30/07, Christian Defoy <ch...@gmail.com> wrote:
> > Hello,
> >
> > I have a Shape object that is referred to by an Attachment object.
> > The ID for Attachment is computed based on the ID of the related Shape
> > and the database enforces that by having the Attachment.shape_id field
> > as a computed field.
> >
> > create table Shape
> > (
> >     shape_id int identity not null primary key
> > );
> >
> > create table Attachment
> > (
> >     attachment_id int not null primary key,
> >     shape_id as (attachment_id / 65536),
> >     name varchar(20)
> > );
> >
> > (Note: the DB is SQL Server)
> >
> > The Attachment object has a reference to the Shape object but I must
> > prevent OpenJPA from trying to persist that reference to the database.
> >  However, OpenJPA must be able to load the Shape when I load the
> > Attachment. Is that possible?  If so, how do I do that?
> >
> > I use the following annotations in Attachment to refer to Shape, but
> > that only gets me an exception.
> >
> >     @ManyToOne( fetch=FetchType.LAZY )
> >     @Column( name="shape_id" )
> >     private Shape shape;
> >
> > The exception is:
> >
> > org.apache.openjpa.lib.jdbc.ReportingSQLException: Column 'shape_id'
> > cannot be modified because it is a computed column. {prepstmnt
> > 30638546 INSERT INTO attachment (attachment_id, name, shape_id) VALUES
> > (?, ?, ?) [params=(int) 1900544, (String) MyAttach, (int) 29]}
> > [code=271, state=S0001]
> >
> > Thanks in advance!
> >
> > Christian
> >
>
>
> --
> Patrick Linskey
> 202 669 5907
>

Re: Skip a computed field on insert and update

Posted by Patrick Linskey <pl...@gmail.com>.
Hi,

I think that you want to do the following:

>     @ManyToOne( fetch=FetchType.LAZY )
>     @Column( name="shape_id" )
>     private Shape shape;

@Column( name="shape_id", insertable=false, updatable=false )

That will tell OpenJPA to not insert or update those columns.

-Patrick

On 8/30/07, Christian Defoy <ch...@gmail.com> wrote:
> Hello,
>
> I have a Shape object that is referred to by an Attachment object.
> The ID for Attachment is computed based on the ID of the related Shape
> and the database enforces that by having the Attachment.shape_id field
> as a computed field.
>
> create table Shape
> (
>     shape_id int identity not null primary key
> );
>
> create table Attachment
> (
>     attachment_id int not null primary key,
>     shape_id as (attachment_id / 65536),
>     name varchar(20)
> );
>
> (Note: the DB is SQL Server)
>
> The Attachment object has a reference to the Shape object but I must
> prevent OpenJPA from trying to persist that reference to the database.
>  However, OpenJPA must be able to load the Shape when I load the
> Attachment. Is that possible?  If so, how do I do that?
>
> I use the following annotations in Attachment to refer to Shape, but
> that only gets me an exception.
>
>     @ManyToOne( fetch=FetchType.LAZY )
>     @Column( name="shape_id" )
>     private Shape shape;
>
> The exception is:
>
> org.apache.openjpa.lib.jdbc.ReportingSQLException: Column 'shape_id'
> cannot be modified because it is a computed column. {prepstmnt
> 30638546 INSERT INTO attachment (attachment_id, name, shape_id) VALUES
> (?, ?, ?) [params=(int) 1900544, (String) MyAttach, (int) 29]}
> [code=271, state=S0001]
>
> Thanks in advance!
>
> Christian
>


-- 
Patrick Linskey
202 669 5907