Re: Select Query on Table with no Primary Key

From: Craig L Russell (Craig.Russel..un.COM)
Date: Fri Jun 08 2007 - 00:01:56 EDT

  • Next message: Andrus Adamchik: "Re: Select Query on Table with no Primary Key"

    Just FYI, when JDO reads data from tables without PK, it internally
    creates a unique id, similar to a generated PK, for the objects that
    it reads and these ids are discarded when no longer needed. The fact
    that the mapping is for tables without PK is known by the code that
    creates the temporary ids.

    Craig

    On Jun 7, 2007, at 2:33 AM, Dave Merrin wrote:

    > OK, I've stepped through the code and got to the ObjectResolver. In
    > there it checks for primary keys and throws an exception if there
    > aren't any. Is there any way I can override this code easily to
    > removed the primary key check?
    >
    >
    > ObjectResolver(DataContext context, ObjEntity entity, boolean
    > refresh,
    > boolean resolveInheritanceHierarchy) {
    > init(context, entity, refresh, resolveInheritanceHierarchy);
    > }
    >
    > void init(
    > DataContext context,
    > ObjEntity entity,
    > boolean refresh,
    > boolean resolveInheritanceHierarchy) {
    > // sanity check
    > DbEntity dbEntity = entity.getDbEntity();
    > if (dbEntity == null) {
    > throw new CayenneRuntimeException("ObjEntity '"
    > + entity.getName()
    > + "' has no DbEntity.");
    > }
    >
    > this.primaryKey = dbEntity.getPrimaryKey();
    > if (primaryKey.size() == 0) {
    > throw new CayenneRuntimeException("Won't be able to
    > create ObjectId for '"
    > + entity.getName()
    > + "'. Reason: DbEntity '"
    > + dbEntity.getName()
    > + "' has no Primary Key defined.");
    > }
    >
    > this.context = context;
    > this.cache = context.getObjectStore().getDataRowCache();
    > this.refreshObjects = refresh;
    > this.entity = entity;
    > this.inheritanceTree = context.getEntityResolver
    > ().lookupInheritanceTree(entity);
    > this.resolveInheritance = (inheritanceTree != null)
    > ? resolveInheritanceHierarchy
    > : false;
    > }
    > Michael Gentry wrote:
    >> The only thing I can think of trying (and maybe Andrus/Tore/Mike/
    >> etc would
    >> have better ideas) is to model that ObjEntity (and perhaps others
    >> it relates
    >> to?) as read-only and see if Cayenne stops caring about missing a
    >> PK. Just
    >> a thought -- I've not tried it.
    >>
    >> /dev/mrg
    >>
    >>
    >> On 6/6/07, Dave Merrin <dmerri..pasystems.co.uk> wrote:
    >>>
    >>> Sorry, I forgot to mention that I won't be updating the data. I
    >>> would
    >>> like to move round the database using relationships though.
    >>>
    >>> Apart from changing the database are there any hacks I could try
    >>> to get
    >>> round this problem.
    >>>
    >>> Cheers,
    >>>
    >>> Dave
    >>>
    >>> Michael Gentry wrote:
    >>> > Cayenne needs PKs in order to do UPDATEs. If you aren't
    >>> modifying the
    >>> > data,
    >>> > you might try fetching as data rows instead of CayenneDataObjects.
    >>> > I'm not
    >>> > certain if that would work, but has a much higher chance. Of
    >>> course,
    >>> you
    >>> > still won't be able to make a CayenneDataObject out of it.
    >>> >
    >>> > /dev/mrg
    >>> >
    >>> >
    >>> > On 6/6/07, Dave Merrin <dmerri..pasystems.co.uk> wrote:
    >>> >>
    >>> >> Hi,
    >>> >>
    >>> >> I'm trying to run a SelectQuery on a table with no primary key.
    >>> >> Unfortunately it's not working. Can anybody help? I have no
    >>> control
    >>> over
    >>> >> the database so I can't add in primary keys.
    >>> >>
    >>> >> Cheers,
    >>> >>
    >>> >> Dave
    >>> >>
    >>> >> INFO QueryLogger: SELECT t0.ID, t0.KIND, t0.TIMESTAMP FROM
    >>> >> MPS.STATETABLE_CPT t0
    >>> >> INFO QueryLogger: === returned 949 rows. - took 203 ms.
    >>> >> INFO QueryLogger: +++ transaction committed.
    >>> >> Exception in thread "main"
    >>> org.apache.cayenne.CayenneRuntimeException:
    >>> >> [v.2.0.3 May 6 2007] Won't be able to create ObjectId for
    >>> >> 'StatetableCpt'. Reason: DbEntity 'STATETABLE_CPT' has no
    >>> Primary Key
    >>> >> defined.
    >>> >> at
    >>> >> org.apache.cayenne.access.ObjectResolver.init
    >>> (ObjectResolver.java:75)
    >>> >> at
    >>> >> org.apache.cayenne.access.ObjectResolver.<init>
    >>> (ObjectResolver.java:57)
    >>> >> at
    >>> >>
    >>> org.apache.cayenne.access.DataDomainQueryAction.interceptObjectConve
    >>> rsion(
    >>> >>
    >>> >> DataDomainQueryAction.java:319)
    >>> >> at
    >>> >> org.apache.cayenne.access.DataDomainQueryAction.execute(
    >>> >> DataDomainQueryAction.java:116)
    >>> >> at org.apache.cayenne.access.DataDomain.onQuery
    >>> (DataDomain.java
    >>> >> :746)
    >>> >> at
    >>> >> org.apache.cayenne.util.ObjectContextQueryAction.runQuery(
    >>> >> ObjectContextQueryAction.java:217)
    >>> >> at
    >>> >> org.apache.cayenne.access.DataContextQueryAction.execute(
    >>> >> DataContextQueryAction.java:54)
    >>> >> at
    >>> >> org.apache.cayenne.access.DataContext.onQuery(DataContext.java:
    >>> 1387)
    >>> >> at
    >>> >> org.apache.cayenne.access.DataContext.performQuery
    >>> (DataContext.java
    >>> :1376)
    >>> >>
    >>> >> at
    >>> >> ipa.printexpress.datahelpers.Session.fetchEntityCollection
    >>> (Session.java
    >>> >> :158)
    >>> >> at
    >>> >> ipa.px.importer.abb.ABBImporterMain.main(ABBImporterMain.java:
    >>> 109)
    >>> >>
    >>> >
    >>>
    >>>
    >>
    >

    Craig Russell
    Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
    408 276-5638 mailto:Craig.Russel..un.com
    P.S. A good JDO? O, Gasp!





    This archive was generated by hypermail 2.0.0 : Fri Jun 08 2007 - 00:02:26 EDT