You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by it...@daimler.com on 2009/05/22 15:24:05 UTC

@DataStoreId + Generator + omitting any @Id-Fields

Hello,

as I was reading the thread 'Can we have entities without @Id' carefully, 
I read about the possibility to use @DataStoreId to modify the way a 
primary key column can be retrieved. However, I do NOT understand 
correctly how to do this. 

I have an issue where I'm not able to change the table-model within the 
database, but have to create entities from a table that does not provide a 
primary key at all. As it is simply necessary to read information from 
this table I wanted to use DataStoreId to let JPA generate an Id Column 
itself and add it to the entity.

However, this does not seem to work as I expected. I added the following 
code:

@Entity
@Table(name = "WBZ")
@DataStoreId(strategy = GenerationType.SEQUENCE, generator="time")
public class WirtschaftlicheBeziehung 
{
        // no id fields in here
}

As far as I can see in the openJPA source, this leads to an automatic 
generation of an 'id' column. My problem now is, I do not have an 'id' 
column in the database, thus this crashes with an exception, that the 
table WBZ lacks a column named 'id'. 

My question now is, is there a way to use DataStoreId to handle tables 
without primary key columns? I do not fully understand the usage of the 
special annotation of DataStoreId when it still searches for a column in 
the database. I thought I do not have to have such a column?

Thanks for your time!

Heiko

If you are not the intended addressee, please inform us immediately that you have received this e-mail in error, and delete it. We thank you for your cooperation.  

Two Entities in an inheritance strategy InheritanceStrategy.JOINED -> always uses most specific Entity

Posted by it...@daimler.com.
Hello,

In our database model, we have a relationship between a table called 
'Partner' and a table called 'Vertriebspartner' that specializes the first 
by adding more information to the 'Partner'. However, there is not always 
an entry in the table 'Vertriebspartner' for every 'Partner'.

In my model I currently have the following inheritance relation defined 
for these two tables:

@Entity
@Table(name = "PARTNER", schema = "PART")
public class Partner
{
    @Id
    @Column(name = "PART_KEY")
    private BigDecimal partKey;
}

@Entity
@Table(name = "VERTRIEBSPARTNER")
@Inheritance(strategy = InheritanceType.JOINED)
@AttributeOverride(name="version", column=@Column(name="VERSION")) 
public class Vertriebspartner extends Partner 
{
}

I now have the problem, that in case I use a 'Partner' in a OneToMany or 
ManyToOne-Relationship, openJPA always tries (even though i use Partner 
and NOT Vertriebspartner) to join both. This leads to missing entries, as 
the join often leads to no entry at all, but there is definilty a Partner.

Thus:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PART_KEY", table = "WBZ_V")
private Partner partner;

ends in a select that joins Partner and Vertriebspartner. I even tried to 
apply targetEntity = Partner.class to the ManyToOne, but that did not help 
at all.

What do I do wrong here? Is the joined inheritance strategy not ment to be 
used when there is not always an entry available in the more specialized 
table? Is this maybe a problem in openJPA?

Thanks,

Heiko

If you are not the intended addressee, please inform us immediately that you have received this e-mail in error, and delete it. We thank you for your cooperation.  

[SOLVED] Re: Re: @DataStoreId + Generator + omitting any @Id-Fields

Posted by it...@daimler.com.
Hey Kevin,

thank you, that clarifies it definitly. My problem now is, that as I'm 
unable to change the schema in the database by adding the missing 
PK-field, I've to
use a native query to retrieve the values. Maybe I can convince the guys 
that maintain the database to rethink their entities, as the entity I need 
to
retrieve is not week, thus the relationship model is not fulfiled at all.

Thanks again for clarifying things,

Heiko

kwsutter@gmail.com schrieb am 22.05.2009 20:23:54:

> Hi Heiko,
> My apologies if my other reply implied that no PK would be required. 
What
> the @DataStoreId provides is the ability to hide an PK field from the 
Entity
> definition.  But, JPA itself (and OpenJPA) requires a PK field of some 
sort
> in order to perform normal Entity operations (insert, find, etc).  Hope 
this
> helps clarify the usage.
> 
> Kevin
> 
> 
> On Fri, May 22, 2009 at 8:24 AM, <it...@daimler.com> wrote:
> 
> > Hello,
> >
> > as I was reading the thread 'Can we have entities without @Id' 
carefully,
> > I read about the possibility to use @DataStoreId to modify the way a
> > primary key column can be retrieved. However, I do NOT understand
> > correctly how to do this.
> >
> > I have an issue where I'm not able to change the table-model within 
the
> > database, but have to create entities from a table that does not 
provide a
> > primary key at all. As it is simply necessary to read information from
> > this table I wanted to use DataStoreId to let JPA generate an Id 
Column
> > itself and add it to the entity.
> >
> > However, this does not seem to work as I expected. I added the 
following
> > code:
> >
> > @Entity
> > @Table(name = "WBZ")
> > @DataStoreId(strategy = GenerationType.SEQUENCE, generator="time")
> > public class WirtschaftlicheBeziehung
> > {
> >        // no id fields in here
> > }
> >
> > As far as I can see in the openJPA source, this leads to an automatic
> > generation of an 'id' column. My problem now is, I do not have an 'id'
> > column in the database, thus this crashes with an exception, that the
> > table WBZ lacks a column named 'id'.
> >
> > My question now is, is there a way to use DataStoreId to handle tables
> > without primary key columns? I do not fully understand the usage of 
the
> > special annotation of DataStoreId when it still searches for a column 
in
> > the database. I thought I do not have to have such a column?
> >
> > Thanks for your time!
> >
> > Heiko
> >
> > If you are not the intended addressee, please inform us immediately 
that
> > you have received this e-mail in error, and delete it. We thank you 
for your
> > cooperation.


If you are not the intended addressee, please inform us immediately that you have received this e-mail in error, and delete it. We thank you for your cooperation.  

Re: @DataStoreId + Generator + omitting any @Id-Fields

Posted by Kevin Sutter <kw...@gmail.com>.
Hi Heiko,
My apologies if my other reply implied that no PK would be required.  What
the @DataStoreId provides is the ability to hide an PK field from the Entity
definition.  But, JPA itself (and OpenJPA) requires a PK field of some sort
in order to perform normal Entity operations (insert, find, etc).  Hope this
helps clarify the usage.

Kevin


On Fri, May 22, 2009 at 8:24 AM, <it...@daimler.com> wrote:

> Hello,
>
> as I was reading the thread 'Can we have entities without @Id' carefully,
> I read about the possibility to use @DataStoreId to modify the way a
> primary key column can be retrieved. However, I do NOT understand
> correctly how to do this.
>
> I have an issue where I'm not able to change the table-model within the
> database, but have to create entities from a table that does not provide a
> primary key at all. As it is simply necessary to read information from
> this table I wanted to use DataStoreId to let JPA generate an Id Column
> itself and add it to the entity.
>
> However, this does not seem to work as I expected. I added the following
> code:
>
> @Entity
> @Table(name = "WBZ")
> @DataStoreId(strategy = GenerationType.SEQUENCE, generator="time")
> public class WirtschaftlicheBeziehung
> {
>        // no id fields in here
> }
>
> As far as I can see in the openJPA source, this leads to an automatic
> generation of an 'id' column. My problem now is, I do not have an 'id'
> column in the database, thus this crashes with an exception, that the
> table WBZ lacks a column named 'id'.
>
> My question now is, is there a way to use DataStoreId to handle tables
> without primary key columns? I do not fully understand the usage of the
> special annotation of DataStoreId when it still searches for a column in
> the database. I thought I do not have to have such a column?
>
> Thanks for your time!
>
> Heiko
>
> If you are not the intended addressee, please inform us immediately that
> you have received this e-mail in error, and delete it. We thank you for your
> cooperation.