Re: Funky Query from entityManager.find(Class,id)

2007-04-24 Thread Marc Prud'hommeaux

Phill-

On Apr 24, 2007, at 11:42 AM, Phill Moran wrote:

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?


Whenever possible, yes. For all fetch=EAGER relations (which  
@OneToOne and @ManyToOne default to), OpenJPA will try to eagerly  
select all of the instances in as few statements as possible. Most of  
the time it will just be a single statement.


This is discussed in some detail at:

  http://incubator.apache.org/openjpa/docs/latest/manual/ 
manual.html#ref_guide_perfpack_eager




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:[EMAIL PROTECTED] 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"),
  

RE: Funky Query from entityManager.find(Class,id)

2007-04-24 Thread Phill Moran
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:[EMAIL PROTECTED] 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"),
>  

Re: Funky Query from entityManager.find(Class,id)

2007-04-24 Thread Marc Prud'hommeaux

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.persi

Funky Query from entityManager.find(Class,id)

2007-04-23 Thread Phill Moran
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 persis