-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: HQL returning null but values in the database
PostPosted: Mon Nov 02, 2009 11:43 am 
Newbie

Joined: Mon Nov 02, 2009 11:29 am
Posts: 1
I'm using Hibernate with JPA annotations and I will run a HQL query in debugger that wont return any results. The strange thing is that after I attempt to run the HQL query I
immediately stop the debugger and plug the generated mysql code generated from HQL into a query browser and it returns the data fine. So HQL doesn't work but the mysql code it generates does.

The HQL statement:

if(newInstance.getIdent()!=null){
session.flush();

org.hibernate.Query SQL_Query=session.createQuery("select metaData from Customer as customer " +
" join customer.customerMetaData as metaData " +
" inner join customer.customerMetaData as metaData " +
" inner join metaData.customerMetaDataName as metaName "+
" where customer.ident=? and metaData.customerMetaDataName.stringValue =?");

SQL_Query.setInteger(0, newInstance.getIdent());
SQL_Query.setString(1, c.getCustomerMetaDataName().getStringValue());

SQL_Query.setMaxResults(1);
metaData=(CustomerMetaData) SQL_Query.uniqueResult();


}


I want to return CustomerMetaData. Customer contains a set of CustomerMetaData and CustomerMetaData contains a CustomerMetaDataName and CustomerMetaDataValue. The following is relevant code.

Customer.java
@Entity
public class Customer implements Serializable, Comparable{
@ManyToMany
@JoinTable(name = "Customer_CustomerPreference",
joinColumns = {
@JoinColumn(name = "Customer_id", nullable = false)
}, inverseJoinColumns = {
@JoinColumn(name = "CustomerPreference_id",nullable = false)
}, uniqueConstraints = {
@UniqueConstraint(columnNames = {"Customer_id", "CustomerPreference_id"})
})
private List<CustomerPreference> customerPreferences;
@Id
@GeneratedValue
private Integer ident;

@ManyToOne(fetch=FetchType.EAGER)
private CustomerLastName lastName;


@ManyToOne(fetch=FetchType.EAGER)
private CustomerFirstName firstName;

@ManyToOne(fetch=FetchType.EAGER)
private CustomerMiddleName middleName;

@OneToMany(fetch=FetchType.EAGER)
@JoinTable(name="Customer_CustomerMetaData",
joinColumns={@JoinColumn(name="Customer_id")},
inverseJoinColumns = {@JoinColumn(name="CustomerMetaData_id")}
)
private Set<CustomerMetaData> customerMetaData;

public Customer(){
middleName=new CustomerMiddleName();
customerPreferences = new LinkedList<CustomerPreference>();
customerMetaData = new HashSet<CustomerMetaData>();
}

@Override
public int compareTo(Object arg0) {
return getLastName().compareTo(((Customer)arg0).getLastName());
}

/**
* @return the ident
*/
public Integer getIdent() {
return ident;
}

/**
* @param ident the ident to set
*/
public void setIdent(Integer ident) {
this.ident = ident;
}

/**
* @return the lastName
*/
public CustomerLastName getLastName() {
return lastName;
}

/**
* @param lastName the lastName to set
*/
public void setLastName(CustomerLastName lastName) {
this.lastName = lastName;
}

/**
* @return the firstName
*/
public CustomerFirstName getFirstName() {
return firstName;
}

/**
* @param firstName the firstName to set
*/
public void setFirstName(CustomerFirstName firstName) {
this.firstName = firstName;
}

/**
* @return the middleName
*/
public CustomerMiddleName getMiddleName() {
return middleName;
}

/**
* @param middleName the middleName to set
*/
public void setMiddleName(CustomerMiddleName middleName) {
this.middleName = middleName;
}

/**
* @return the customerMetaData
*/
public Set<CustomerMetaData> getCustomerMetaData() {
return customerMetaData;
}

/**
* @param customerMetaData the customerMetaData to set
*/
public void setCustomerMetaData(final Set<CustomerMetaData> customerMetaData) {
if (customerMetaData instanceof SortedSet) {
this.customerMetaData = customerMetaData;
} else {
this.customerMetaData = new TreeSet<CustomerMetaData>(customerMetaData);
}
}

/**
* @return the clientPreferences
*/
public List<CustomerPreference> getCustomerPreferences() {
return customerPreferences;
}

/**
* @param clientPreferences the clientPreferences to set
*/
public void setCustomerPreferences(List<CustomerPreference> clientPreferences) {
this.customerPreferences = clientPreferences;
}
}


CustomerMetaData.java
*/
@Entity
public class CustomerMetaData implements Serializable,Comparable{

@Id
@GeneratedValue
private Integer ident;

@ManyToOne(fetch=FetchType.EAGER)
@JoinTable(name="CustomerMetaData_CustomerMetaDataName",
joinColumns={@JoinColumn(name="customerMetaData_id")},
inverseJoinColumns = {@JoinColumn(name="customerMetaDataName_id")}
)
private CustomerMetaDataName customerMetaDataName;

@ManyToOne(fetch=FetchType.EAGER)
@JoinTable(name="CustomerMetaData_CustomerMetaDataValue",
joinColumns={@JoinColumn(name="customerMetaData_id")},
inverseJoinColumns = {@JoinColumn(name="customerMetaDataValue_id")}
)
private CustomerMetaDataValue customerMetaDataValue;

@Override
public int compareTo(Object arg0) {
return getCustomerMetaDataName().
compareTo(((CustomerMetaData)arg0).getCustomerMetaDataName());
}
@Override
public String toString(){
return this.getCustomerMetaDataName() + ":" + this.getCustomerMetaDataValue();
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CustomerMetaData other = (CustomerMetaData) obj;
if (this.customerMetaDataName != other.customerMetaDataName && (this.customerMetaDataName == null || !this.customerMetaDataName.equals(other.customerMetaDataName))) {
return false;
}
if (this.customerMetaDataValue != other.customerMetaDataValue && (this.customerMetaDataValue == null || !this.customerMetaDataValue.equals(other.customerMetaDataValue))) {
return false;
}
return true;
}

@Override
public int hashCode() {
int hash = 3;
hash = 23 * hash + (this.customerMetaDataName != null ? this.customerMetaDataName.hashCode() : 0);
hash = 23 * hash + (this.customerMetaDataValue != null ? this.customerMetaDataValue.hashCode() : 0);
return hash;
}


/**
* @return the ident
*/
public Integer getIdent() {
return ident;
}

/**
* @param ident the ident to set
*/
public void setIdent(Integer ident) {
this.ident = ident;
}

public CustomerMetaData(){

}

public CustomerMetaData(
CustomerMetaDataName name,
CustomerMetaDataValue value){
setCustomerMetaDataName(name);
setCustomerMetaDataValue(value);
}

/**
* @return the customerMetaDataName
*/
public CustomerMetaDataName getCustomerMetaDataName() {
return customerMetaDataName;
}

/**
* @param customerMetaDataName the customerMetaDataName to set
*/
public void setCustomerMetaDataName(CustomerMetaDataName customerMetaDataName) {
this.customerMetaDataName = customerMetaDataName;
}

/**
* @return the customerMetaDataValue
*/
public CustomerMetaDataValue getCustomerMetaDataValue() {
return customerMetaDataValue;
}

/**
* @param customerMetaDataValue the customerMetaDataValue to set
*/
public void setCustomerMetaDataValue(CustomerMetaDataValue customerMetaDataValue) {
this.customerMetaDataValue = customerMetaDataValue;
}

}






CustomerMetaDataName.java

@Entity
public class CustomerMetaDataName
implements Serializable, Comparable {

@Id
@GeneratedValue
private Integer ident;
@Column(unique=true,nullable = false, length = 100)
private String stringValue;

@Override
public int compareTo(Object arg0) {
return this.getStringValue().
compareToIgnoreCase(((CustomerMetaDataName) arg0).getStringValue());
}

public String toString(){
return this.stringValue;
}

/**
* @return the ident
*/
public Integer getIdent() {
return ident;
}

/**
* @param ident the ident to set
*/
public void setIdent(Integer ident) {
this.ident = ident;
}

/**
* @return the stringValue
*/
public String getStringValue() {
return stringValue;
}

/**
* @param stringValue the stringValue to set
*/
public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}

@Override
public boolean equals(Object o){
if(o.getClass()!=this.getClass()){
return false;
}else{
return o.hashCode()==this.hashCode();
}
}

@Override
public int hashCode() {
int hash = 8;
hash = 3 * hash + (this.stringValue != null ? this.stringValue.hashCode() : 0);
return hash;
}
}





CustomerMetaDavaValue.java
@Entity
public class CustomerMetaDataValue
implements Serializable, Comparable {

@Id
@GeneratedValue
private Integer ident;
@Column(unique=true,nullable = false, length = 100)
private String stringValue;

@Override
public int compareTo(Object arg0) {
return this.getStringValue().
compareToIgnoreCase(((CustomerMetaDataValue) arg0).getStringValue());
}

public String toString(){
return this.stringValue;
}

/**
* @return the ident
*/
public Integer getIdent() {
return ident;
}

/**
* @param ident the ident to set
*/
public void setIdent(Integer ident) {
this.ident = ident;
}

/**
* @return the stringValue
*/
public String getStringValue() {
return stringValue;
}

/**
* @param stringValue the stringValue to set
*/
public void setStringValue(String stringValue) {
this.stringValue = stringValue;
}

@Override
public boolean equals(Object o){
if(o.getClass()!=this.getClass()){
return false;
}else{
return o.hashCode()==this.hashCode();
}
}

@Override
public int hashCode() {
int hash = 42;
hash = 23 * hash + (this.stringValue != null ? this.stringValue.hashCode() : 0);
return hash;
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.