Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate exception

 
Ambiga kannan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using springs framework and i am using Hibernate annotation .I have a DAO class named Transaction header which has composite primary key.

When i try to select any of the primary key in the query object i am getting error

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76)

When I exclude the primary keys its working fine

This is my DAO class

public class TransactionValidationDAO {

/**
* Serial version Id.
*/
private static final long serialVersionUID = -2275559064196920416L;
private EntityManager entityManager;
private static org.apache.log4j.Logger log = Logger.getLogger(DeviceDao.class);

public Object[] exists(String transNumber) {
List valList = new ArrayList();
Object valObj[]= null ;
Query query = getEntityManager().createQuery(
"select b.orguCodeId,b.txnTradingDate,b.txnOpcode,b.txnExternalRef from TransactionHeader b where b.txnState = '15' and b.txnExternalRef = '"+transNumber+"' and " +
"b.txnType='95'");

valList = query.getResultList();
if (valList != null &&valList.size() > 0){
valObj = (Object[]) valList.get(0);
}
return valObj;
}

/**
* @return the entityManager
*/
public EntityManager getEntityManager() {
return entityManager;
}

/**
* @param entityManager
* the entityManager to set
*/
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

}

TransactionHeader Class(to access the TXN_HEADER table)

@Entity
@IdClass(TransactionKey.class)
@Table(name = "TXN_HEADER")
public class TransactionHeader implements Serializable {

/**
* Serial version Id.
*/
private static final long serialVersionUID = -5011704205640463450L;


@Id@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ORGU_CODE_CMPY")
protected String orgCodeCmpId;

@Id@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ORGU_CODE")
protected String orguCodeId;

@Id@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "TILL_SHORT_DESC")
protected String tillShrtDescId;

@Id@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "TXHD_TXN_NR")
protected String txnNumberId;

@Column(name = "TXHD_EXTERNAL_REF")
protected String txnExternalRef;
@Column(name = "TXHD_STATE")
protected String txnState;
@Column(name = "TXHD_TXN_TYPE")
protected String txnType;
@Column(name = "TXHD_TRADING_DATE")
protected String txnTradingDate;
@Column(name = "TXHD_OPERATOR_CODE")
protected String txnOpcode;
@Column(name = "TXDE_DETAIL_NR")
protected String txnDetailNr;

@OneToMany(mappedBy="transactionHeader")
private Collection <TransactionDetMobile>transactionDetMobile;
@OneToMany(mappedBy="transactionHeader")
private Collection <TransactionDetail>transactionDetail;

public String getTxnState() {
return txnState;
}
public void setTxnState(String txnState) {
this.txnState = txnState;
}
public String getTxnTradingDate() {
return txnTradingDate;
}
public void setTxnTradingDate(String txnTradingDate) {
this.txnTradingDate = txnTradingDate;
}
public String getTxnType() {
return txnType;
}
public void setTxnType(String txnType) {
this.txnType = txnType;
}
public String getTxnOpcode() {
return txnOpcode;
}
public void setTxnOpcode(String txnOpcode) {
this.txnOpcode = txnOpcode;
}
public String getTxnExternalRef() {
return txnExternalRef;
}
public void setTxnExternalRef(String txnExternalRef) {
this.txnExternalRef = txnExternalRef;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getOrgCodeCmpId() {
return orgCodeCmpId;
}
public void setOrgCodeCmpId(String orgCodeCmpId) {
this.orgCodeCmpId = orgCodeCmpId;
}

public String getOrguCodeId() {
return orguCodeId;
}
public void setOrguCodeId(String orguCodeId) {
this.orguCodeId = orguCodeId;
}
public String getTillShrtDescId() {
return tillShrtDescId;
}
public void setTillShrtDescId(String tillShrtDescId) {
this.tillShrtDescId = tillShrtDescId;
}
public String getTxnNumberId() {
return txnNumberId;
}
public void setTxnNumberId(String txnNumberId) {
this.txnNumberId = txnNumberId;
}


public Collection<TransactionDetail> getTransactionDetail() {
return transactionDetail;
}
public void setTransactionDetail(Collection<TransactionDetail> transactionDetail) {
this.transactionDetail = transactionDetail;
}
public String getTxnDetailNr() {
return txnDetailNr;
}
public void setTxnDetailNr(String txnDetailNr) {
this.txnDetailNr = txnDetailNr;
}
public Collection<TransactionDetMobile> getTransactionDetMobile() {
return transactionDetMobile;
}
public void setTransactionDetMobile(
Collection<TransactionDetMobile> transactionDetMobile) {
this.transactionDetMobile = transactionDetMobile;
}

}


TransactionKey.class(to get compound primary key)

public final class TransactionKey implements Serializable {
public String orgCodeCmpId;
public String orguCodeId;
public String tillShrtDescId;
public String txnNumberId;

public TransactionKey() {}

public TransactionKey(String orgCodeCmpId,String orguCodeId,String tillShrtDescId,String txnNumberId) {
this.orgCodeCmpId = orgCodeCmpId;
this.orguCodeId = orguCodeId;
this.tillShrtDescId = tillShrtDescId;
this.txnNumberId = txnNumberId;

}

public boolean equals(Object otherOb) {
if (this == otherOb) {
return true;
}
if (!(otherOb instanceof TransactionKey )) {
return false;
}

if(otherOb == null) return false;

TransactionKey other = (TransactionKey) otherOb;
return
other.orgCodeCmpId.equals(orgCodeCmpId) && other.orguCodeId.equals(orguCodeId)
&& other.tillShrtDescId.equals(tillShrtDescId) && other.txnNumberId.equals(txnNumberId);

}

public int hashCode() {
return (int) orgCodeCmpId.hashCode();
}

public String getOrgCodeCmpId() {
return orgCodeCmpId;
}

public void setOrgCodeCmpId(String orgCodeCmpId) {
this.orgCodeCmpId = orgCodeCmpId;
}

public String getOrguCodeId() {
return orguCodeId;
}

public void setOrguCodeId(String orguCodeId) {
this.orguCodeId = orguCodeId;
}

public String getTillShrtDescId() {
return tillShrtDescId;
}

public void setTillShrtDescId(String tillShrtDescId) {
this.tillShrtDescId = tillShrtDescId;
}

public String getTxnNumberId() {
return txnNumberId;
}

public void setTxnNumberId(String txnNumberId) {
this.txnNumberId = txnNumberId;
}

}




In application context i am using :



<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/CDB" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

Primary keys are not getting identified.I am struggling to resolve it,can any one please help me to fix it.

Thanks,
Ambiga.
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the query you are writing which is giving error.???
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...
 
Ambiga kannan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Singh wrote:What is the query you are writing which is giving error.???



I am using the below hql query in which orguCodeId is one of the primary key.

"select b.orguCodeId,b.txnTradingDate,b.txnOpcode,b.txnExternalRef from TransactionHeader b where b.txnState = '15' and b.txnExternalRef = '"+transNumber+"' and " +
"b.txnType='95'"



if exclude that id and give as below one i am able to get the result.

"select b.txnTradingDate,b.txnOpcode,b.txnExternalRef from TransactionHeader b where b.txnState = '15' and b.txnExternalRef = '"+transNumber+"' and " +
"b.txnType='95'"

i am not able to retrieve the primary key values.Please suggest me a solution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic