I'm using Hibernate 2.1.1.
I'm trying to switch my "session.load"'s to "session.get"'s. But it's giving me "NullPointerException". Here is the case:
1) The following code works correctly:
OpControloSq0589 op_controlo = (OpControloSq0589)session.load(OpControloSq0589.class, op_id);
2) But this one doesn't (chould give the same result):
if((OpControloSq0589)session.get(OpControloSq0589.class, op_id) == null) {
System.out.println("CONTAO NAO EXISTE");
}
----------------------------------------------------
Mapping files (generated with Middlegen R3)
----------------------------------------------------
OpControloSq0589
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="pt.vo.OpControloSq0589"
table="op_controlo_sq058_9"
>
<id
name="opFk"
type="long"
column="op_fk"
>
<generator class="assigned" />
</id>
<property
name="qtdProduzida"
type="int"
column="qtd_produzida"
length="4"
/>
<!-- associations -->
<!-- bi-directional one-to-one association to Op -->
<one-to-one
name="op"
class="pt.vo.Op"
outer-join="auto"
/>
</class>
</hibernate-mapping>
Op
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin
http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class
name="pt.vo.Op"
table="op"
>
<id
name="id"
type="long"
column="id"
>
<generator class="increment" />
</id>
<property
name="quantidade"
type="int"
column="quantidade"
length="4"
/>
<!-- associations -->
<!-- bi-directional one-to-one association to OpControloSq0589 -->
<one-to-one
name="opControloSq0589"
class="pt.vo.OpControloSq0589"
outer-join="auto"
constrained="false"
/>
</class>
</hibernate-mapping>
----------
POJO's
----------
OpControloSq0589.java
package pt.vo;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class OpControloSq0589 implements Serializable {
/** identifier field */
private Long opFk;
/** nullable persistent field */
private int qtdProduzida;
/** nullable persistent field */
private pt.vo.Op op;
/** full constructor */
public OpControloSq0589(Long opFk, int qtdProduzida, pt.vo.Op op) {
this.opFk = opFk;
this.qtdProduzida = qtdProduzida;
this.op = op;
}
/** default constructor */
public OpControloSq0589() {
}
public Long getOpFk() {
return this.opFk;
}
public void setOpFk(Long opFk) {
this.opFk = opFk;
}
public int getQtdProduzida() {
return this.qtdProduzida;
}
public void setQtdProduzida(int qtdProduzida) {
this.qtdProduzida = qtdProduzida;
}
public pt.vo.Op getOp() {
return this.op;
}
public void setOp(pt.vo.Op op) {
this.op = op;
}
public String toString() {
return new ToStringBuilder(this)
.append("opFk", getOpFk())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof OpControloSq0589) ) return false;
OpControloSq0589 castOther = (OpControloSq0589) other;
return new EqualsBuilder()
.append(this.getOpFk(), castOther.getOpFk())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getOpFk())
.toHashCode();
}
}
Op.java
package pt.vo;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class Op implements Serializable {
/** identifier field */
private Long id;
/** nullable persistent field */
private int quantidade;
/** nullable persistent field */
private pt.vo.OpControloSq0589 opControloSq0589;
/** full constructor */
public Op(int quantidade, pt.vo.OpControloSq0589 opControloSq0589) {
this.quantidade = quantidade;
this.opControloSq0589 = opControloSq0589;
}
/** default constructor */
public Op() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public int getQuantidade() {
return this.quantidade;
}
public void setQuantidade(int quantidade) {
this.quantidade = quantidade;
}
public pt.vo.OpControloSq0589 getOpControloSq0589() {
return this.opControloSq0589;
}
public void setOpControloSq0589(pt.vo.OpControloSq0589 opControloSq0589) {
this.opControloSq0589 = opControloSq0589;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof Op) ) return false;
Op castOther = (Op) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getId())
.toHashCode();
}
}