Return-Path: Delivered-To: apmail-incubator-open-jpa-dev-archive@locus.apache.org Received: (qmail 65255 invoked from network); 24 Apr 2007 18:43:34 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 24 Apr 2007 18:43:34 -0000 Received: (qmail 84200 invoked by uid 500); 24 Apr 2007 18:43:30 -0000 Delivered-To: apmail-incubator-open-jpa-dev-archive@incubator.apache.org Received: (qmail 84175 invoked by uid 500); 24 Apr 2007 18:43:30 -0000 Mailing-List: contact open-jpa-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: open-jpa-dev@incubator.apache.org Delivered-To: mailing list open-jpa-dev@incubator.apache.org Received: (qmail 84161 invoked by uid 99); 24 Apr 2007 18:43:30 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 24 Apr 2007 11:43:30 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (herse.apache.org: local policy includes SPF record at spf.trusted-forwarder.org) Received: from [206.190.36.80] (HELO smtp102.rog.mail.re2.yahoo.com) (206.190.36.80) by apache.org (qpsmtpd/0.29) with SMTP; Tue, 24 Apr 2007 11:43:22 -0700 Received: (qmail 96786 invoked from network); 24 Apr 2007 18:43:00 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=rogers.com; h=Received:X-YMail-OSG:From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:X-Mailer:In-reply-to:X-MimeOLE:Thread-Index; b=ZFJjPeKP+1jxbufPT6EN2hwUyAeHARqKd9y9KqRn4KGF4U5hhmaf+TACogwxly7c61kmhmu+sIIeByIWkSh/e+wMdM9kzyigzhyAWrHGztqy2idjp5XWWGVV6fAgYLQGpEOKpAJUDfnQrSjvr5r7IUVLr7nb9UEY5BVFmosovQg= ; Received: from unknown (HELO tyan2400) (pjmoran@rogers.com@74.103.60.187 with login) by smtp102.rog.mail.re2.yahoo.com with SMTP; 24 Apr 2007 18:43:00 -0000 X-YMail-OSG: xUh2Hc8VM1n6iX.URLQsXDXkkDjKNIcBfBRGV81Y1FlcOfb6A4tLGgFQKnUCqP_hlg-- From: "Phill Moran" To: Subject: RE: Funky Query from entityManager.find(Class,id) Date: Tue, 24 Apr 2007 14:42:49 -0400 Message-ID: <001301c786a0$5c8bbea0$6601a8c0@tyan2400> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 In-reply-to: <05DF9B2E-90F9-45FF-BABD-F5D6D44F0865@apache.org> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 Thread-Index: AceGnUZAZzi1i5nPTwCNaFd2yHUl9AAAi7Qw X-Virus-Checked: Checked by ClamAV on apache.org Maybe I am wrong here let me ask a clarifying question. When executing a find on an object that has a related object (like this example) does OpenJPA create one select statement and grab all fields for both objects then parse the results to build both objects? I assumed it makes one select per object. So in this case I would get two selects (which I thought I saw). Phill -----Original Message----- From: Marc Prud'hommeaux [mailto:mprudhomapache@gmail.com] On Behalf Of Marc Prud'hommeaux Sent: April 24, 2007 2:20 PM To: open-jpa-dev@incubator.apache.org Subject: Re: Funky Query from entityManager.find(Class,id) Phill- > It should not be selecting from two tables as it cannot create the > object. As I mentioned before (in the other thread) this causes the > looked for object to be null as if the object was not found. Here is > my call statement ManyToOne and OneToOne relations default to being eagerly fetched. You can override this by specifying fetch=LAZY in the @ManyToOne annotation. That being said, I don't understand that problem. Are you saying that the lookup fails because there is no related attributetype row? That shouldn't prevent the lookup from happening, since we are using an outer join. On Apr 23, 2007, at 11:20 PM, Phill Moran wrote: > I posted this question before but I am now seeing it in several places > and can produce a test case for it. Here are two related classes > (one-to- > many) attribute > and attributeType, each with table/class inheritance. > > > Attribute class: > package ca.BidSpec.emall.categories; > > import javax.persistence.Basic; > import javax.persistence.CascadeType; > import javax.persistence.Column; > import javax.persistence.Entity; > import javax.persistence.Inheritance; > import javax.persistence.InheritanceType; > import javax.persistence.JoinColumn; > import javax.persistence.ManyToOne; > import javax.persistence.NamedQueries; import > javax.persistence.NamedQuery; import javax.persistence.Table; > > import ca.BidSpec.emall.commonServices.PrimaryKey; > import ca.BidSpec.emall.persistence.Persistable; > > @Entity > @Table(name = "attributes") > @NamedQueries( { > @NamedQuery(name = "AttributeFXPK", query = "SELECT a FROM Attribute > a WHERE a.id = :primaryKey"), > @NamedQuery(name = "AttributeFXDescription", query = "SELECT a FROM > Attribute a WHERE a.value = :description ORDER BY a.value") }) > @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class > Attribute extends Persistable { > > private AttributeType type; > > private String value = ""; > > /* > * required for persistence > */ > private Attribute() { > } > > public Attribute(PrimaryKey pk) { > this.setPrimaryKey(pk); > } > > /** > * @param pk > * this objects primary key > * @param type > * this object attribute type > * @param value > * the value for this attribute given it's type > */ > public Attribute(PrimaryKey pk, AttributeType type, String value) { > this.setPrimaryKey(pk); > this.setType(type); > this.setValue(value); > } > > @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.REFRESH, > CascadeType.MERGE }) > @JoinColumn(name = "attributeTypeFK") > public AttributeType getType() { > return type; > } > > public void setType(AttributeType type) { > this.type = type; > } > > @Basic() > @Column(name = "value", nullable = false, length = 50) > public String getValue() { > return value; > } > > public void setValue(String value) { > this.value = value; > } > } > > > AttributeType Class > package ca.BidSpec.emall.categories; > > import javax.persistence.Basic; > import javax.persistence.Column; > import javax.persistence.Entity; > import javax.persistence.Inheritance; > import javax.persistence.InheritanceType; > import javax.persistence.NamedQueries; import > javax.persistence.NamedQuery; import javax.persistence.Table; > > import ca.BidSpec.emall.commonServices.PrimaryKey; > import ca.BidSpec.emall.persistence.Persistable; > > @Entity > @Table(name = "attributetype") > @NamedQueries( { > @NamedQuery(name = "AttributeTypeFXPK", query = "SELECT a FROM > AttributeType a WHERE a.id = :primaryKey"), > @NamedQuery(name = "AttributeTypeFXDescription", query = "SELECT a > FROM AttributeType a WHERE a.description = :description ORDER BY > a.description") }) > @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public class > AttributeType extends Persistable { > private String description = ""; > > /* > * required for persistence > */ > private AttributeType() { > } > > public AttributeType(PrimaryKey pk) { > this.setPrimaryKey(pk); > } > > public AttributeType(PrimaryKey pk, String description) { > this.setPrimaryKey(pk); > this.setDescription(description); > } > > @Basic() > @Column(name = "description", length = 50) > public String getDescription() { > return description; > } > > /* > * Set this attribute type's description > */ > public void setDescription(String description) { > this.description = description; > } > } > > Persistable is my based persistable class of all Entities: > > package ca.BidSpec.emall.persistence; > > import java.io.Serializable; > import java.sql.Timestamp; > import java.util.Date; > > import javax.persistence.Basic; > import javax.persistence.Column; > import javax.persistence.Id; > import javax.persistence.MappedSuperclass; > import javax.persistence.Temporal; > import javax.persistence.TemporalType; import > javax.persistence.Transient; > > import ca.BidSpec.emall.commonServices.PrimaryKey; > > @MappedSuperclass > public abstract class Persistable implements Serializable { > > /** > * Comment for id This is a persistable objects Primary > id > */ > > private String id; > > /** > * User record last updated > */ > > private Timestamp lastUpdated = new Timestamp((new Date()).getTime > ()); > > /** > * @return Returns the persistable objects id. > */ > @Id > @Column(name = "id") > protected String getId() { > return this.id; > } > > protected void setId(String theId) { > this.id = theId; > } > > /** > * @return this object id in a primary id object > */ > @Transient > public PrimaryKey getPrimaryKey() { > return new PrimaryKey(this.getId()); > } > > /** > * @param id > * The id to set. > */ > public void setPrimaryKey(PrimaryKey primaryKey) { > this.setId(primaryKey.getKey()); > } > > /** > * @return Returns the lastUpdated date for this object. > */ > @Basic > @Temporal(TemporalType.TIMESTAMP) > @Column(name = "lastUpdated") > public Timestamp getLastUpdated() { > return this.lastUpdated; > } > > public void setLastUpdated(Timestamp lastUpdated) { > this.lastUpdated = lastUpdated; > } > > public boolean equals(Object obj) { > if ((obj != null) && (obj instanceof Persistable)) { > if (((Persistable) obj).getId().equals(this.getId())) { > return true; > } > } > return false; > } > > public int hashCode() { > return this.getId().hashCode(); > } > > Here is the query generated: > > 16360 TRACE [main] openjpa.jdbc.SQL - > executing prepstmnt 17313093 SELECT t0.id, t0.lastUpdated, t1.id, > t1.lastUpdated, t1.description, t0.value FROM attributes t0 LEFT OUTER > JOIN attributetype t1 ON t0.attributeTypeFK = t1.id WHERE t0.id = ? > [params=(String) 111e3a22-728a-86ce-0126-11a7acec229a:0] > > It should not be selecting from two tables as it cannot create the > object. As I mentioned before (in the other thread) this causes the > looked for object to be null as if the object was not found. Here is > my call statement > > Attribute deleteObj = (Attribute) > categoryFactory.getObjectByPK(Attribute.class, > "111e3a22-728a-86ce-0126-11a7acec229a:0"); <- this ID exists in > the table > assertNotNull(deleteObj); <-- fails here > > getObjectBYPK is > > public Persistable getObjectByPK(Class > objectTypeBeingSought, String pk) { > Persistable per = null; > per = (Persistable) > getEntityManager().find(objectTypeBeingSought, pk); > return per; > } > > } > > I think this may be a bug as I see it in several classes - but > frustratingly not all. Any suggestions > > Phill >