You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by "javadiahad@gmail.com" <ja...@gmail.com> on 2010/09/07 13:42:56 UTC

problem using subclass object as query parameter

Hello everyone
I have a problem using inheritance in OpenJPA.
Here is my code 


@Entity
@Table(name = "BASEBRN")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="TYPE",discriminatorType=DiscriminatorType.INTEGER)
public class BaseBranch  {

	@Id
	@Column(name = "BASEID")
	private Long id;

	@Column(name="TYPE")
	@Enumerated(value = EnumType.ORDINAL)
	private BaseBranchType baseBranchType;

	public BaseBranch(Long id) {
		this.id = id;
	}

	public BaseBranch() {
		super();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	
	public BaseBranchType getBaseBranchType() {
		return baseBranchType;
	}

	public void setBaseBranchType(BaseBranchType baseBranchType) {
		this.baseBranchType = baseBranchType;
	}	

	@Override
	public boolean equals(Object object) {
		if (object != null && !(object instanceof BaseBranch))
			return false;
		BaseBranch other = (BaseBranch) object;
		if (this.id != null && other.id != null
				&& (this.id.longValue() == other.id.longValue()))
			return true;
		return false;

	}

	@Override
	public int hashCode() {
		int hashCode = 0;
		if (id != null)
			hashCode += id.hashCode();
		return hashCode;
	}

}
--------------------------------------------------------------------------------------------

@Table(name = "BRANCH")
@DiscriminatorValue("1")
@PrimaryKeyJoinColumn(name="BRANCHID", referencedColumnName="BASEID")
public class Branch extends BaseBranch {

	@Column(name = "CODE")
	private String code;

	public Branch() {

	}

	public Branch(Long id) {
		this.setId(id);
	}

	public Branch(String code) {
		this.code = code;
	}


    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Branch other = (Branch) obj;
        if ((this.getId() == null) ? (other.getId() != null) :
!this.getId().equals(other.getId())) {
            return false;
        }
        return true;
    }


    public int hashCode() {
        int hash = 7;
        hash = 23 * hash + (this.getId() != null ? this.getId().hashCode() :
0);
        return hash;
    }


}

------------------------------------------------------------------------------------
After runing this code block 

Query q = entityManager.createNamedQuery(""select b from Branch b where
b=:branch"");
q.setParameter("branch", new Branch(1L));
b=q.getResultList();

-----------------------------------------------------------------
result query is something like this

 SELECT t1.BASEID, t1.TYPE, t0.CODE FROM BRANCH t0, BASEBRN t1 WHERE (1 <>
1) AND t1.TYPE = ? AND t0.BRANCHID = t1.BASEID [params=(int) 1]
 ----------------------------------------------------------------
 
 
 OpenJPA never takes the parameter and always raises NoResultException.
 It seems for openJPA both following two params are the same .
 new Branch(1L) 
 and  
 new Branch()
 
 What's wrong with my code or is this maybe an openJPA bug?
 Any idea or help is appreciated
 thanks

-- 
View this message in context: http://openjpa.208410.n2.nabble.com/problem-using-subclass-object-as-query-parameter-tp5506089p5506089.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: problem using subclass object as query parameter

Posted by "javadiahad@gmail.com" <ja...@gmail.com>.
Hi again everyone
I could not resolve this problem.
I have to change all similar queries in my application to something like
this(select b from Branch b where b.id=:branchID),This time it works.
I think this is an openJPA bug .Do you agree?
Anyone can test it .
Thanks 
Ahad
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/problem-using-subclass-object-as-query-parameter-tp5506089p5571555.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: problem using subclass object as query parameter

Posted by "javadiahad@gmail.com" <ja...@gmail.com>.
I am sorry for being late
I removed the type column (private BaseBranchType baseBranchType) form
BaseBranch class but the problem still exists mike.
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/problem-using-subclass-object-as-query-parameter-tp5506089p5545802.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: problem using subclass object as query parameter

Posted by Michael Dick <mi...@gmail.com>.
Sorry it took a little while to get back to this thread.

Is there a reason why you want to expose the discriminator column as a
persistent property (you have the column TYPE mapped as an enum & as the
discriminator col). I think this is causing the problems you're seeing.

I haven't checked the spec to see if this is expressly forbidden, but I'd
definitely say it's unusual.

-mike
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/problem-using-subclass-object-as-query-parameter-tp5506089p5519053.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: problem using subclass object as query parameter

Posted by "javadiahad@gmail.com" <ja...@gmail.com>.
Hi mike
thank you
I am using openJPA 1.2.1 and I also have tested it with 1.2.2 . It doesn't
work with both of them.
I have tested it with runtime and build time enhancement.The problem exists
with both.
and my persistence.xml is something like this

--------------------------------------------------------------------------------------------------------------
<property name="openjpa.jdbc.DBDictionary" value="oracle(batchLimit=0)" />			
<property name="openjpa.jdbc.SynchronizeMappings"
				value="buildSchema(foreignKeys=true,schemaAction='none')" />
<property name="openjpa.jdbc.MappingDefaults"
			
value="ForeignKeyDeleteAction=restrict,JoinForeignKeyDeleteAction=restrict"
/>

<property name="openjpa.RestoreState" value="all" />

<!-- Logging -->
<property name="openjpa.Log" value="Tool=INFO,SQL=TRACE" /> 

<property name="openjpa.TransactionMode" value="managed" />
-------------------------------------------------------------------------------------------------------

Any way I have tested it without any openJPA specific configuration and
I am sure enhancement was done on the entities(I checked it by decompiling)

Best regards
ahad
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/problem-using-subclass-object-as-query-parameter-tp5506089p5510598.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: problem using subclass object as query parameter

Posted by Michael Dick <mi...@gmail.com>.
Hi,

The where clause in the query is obviously wrong :
 WHERE (1 <> 1)     // always returns false

We've seen similar statements with embedded objects on earlier versions of
OpenJPA (1.2.1 comes to mind).

Which version of OpenJPA are you using, and are you doing build time or
runtime enhancement? If you have specified any properties in persistence.xml
please post them and it'll help us reproduce.

Thanks
-mike 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/problem-using-subclass-object-as-query-parameter-tp5506089p5507158.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.