You have an error in your SQL syntax

3 respostas
Z

Bom galera essa ta me arrancando os cabelos, implementei dois entity beans cmp com cmr entre eles, mas quando vou testar da erro, alguém pode me dizer porquê ?Já perdi o maior tempão e não consegui achar…

ai vai o relationship do ejb-jar:

<relationships>
<ejb-relation>
<ejb-relation-name>Customercmp-Order</ejb-relation-name>
<ejb-relationship-role>
<ejb-relationship-role-name>customercmp-spawns-order</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<relationship-role-source>
<ejb-name>Customercmp</ejb-name>
<cmr-field>
<cmr-field-name>order</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</relationship-role-source>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>order-fulfills-customercmp</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
<cascade-delete>true</cascade-delete>
<relationship-role-source>
<ejb-name>Order</ejb-name>

&lt;/relationship-role-source&gt;
     &lt;/ejb-relationship-role&gt;
 &lt;/ejb-relation&gt;

</relationships>

ai vai o relationship do jbosscmp-jdbc:

<relationships>
<ejb-relation>
<ejb-relation-name>Customercmp-Order</ejb-relation-name>
<foreign-key-mapping />
<ejb-relationship-role>
<ejb-relationship-role-name>customercmp-spawns-order</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>username</field-name>
<column-name>USERNAME</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>order-fulfills-customercmp</ejb-relationship-role-name>
<key-fields/>
</ejb-relationship-role>
</ejb-relation>
</relationships>

ai vai o erro :

java.sql.SQLException: Syntax error or access violation,  message from server: You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near order WHERE (USERNAME=mim) at line 1

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1905)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1109)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1203)

at com.mysql.jdbc.Connection.execSQL(Connection.java:2090)

at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1496)

at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:304)

at org.jboss.ejb.plugins.cmp.jdbc.JDBCLoadRelationCommand.execute(JDBCLoadRelationCommand.java:104)

at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.loadRelation(JDBCStoreManager.java:675)

at org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge.load(JDBCCMRFieldBridge.java:1250)

at org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge.getInstanceValue(JDBCCMRFieldBridge.java:633)

at org.jboss.ejb.plugins.cmp.jdbc.CascadeDeleteStrategy$DefaultCascadeDeleteStrategy.removeFromRelations(CascadeDeleteStrategy.java:105)

at org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge.removeFromRelations(JDBCCMRFieldBridge.java:312)

at org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge.removeFromRelations(JDBCEntityBridge.java:312)

at org.jboss.ejb.plugins.cmp.jdbc.JDBCRemoveEntityCommand.execute(JDBCRemoveEntityCommand.java:89)

at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.removeEntity(JDBCStoreManager.java:667)

at org.jboss.ejb.plugins.CMPPersistenceManager.removeEntity(CMPPersistenceManager.java:492)

at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.removeEntity(CachedConnectionInterceptor.java:430)

at org.jboss.ejb.EntityContainer.remove(EntityContainer.java:505)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:324)

at org.jboss.ejb.EntityContainer$ContainerInterceptor.invoke(EntityContainer.java:1084)

at org.jboss.ejb.plugins.cmp.jdbc.JDBCRelationInterceptor.invoke(JDBCRelationInterceptor.java:72)

at org.jboss.ejb.plugins.EntitySynchronizationInterceptor.invoke(EntitySynchronizationInterceptor.java:282)

at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)

at org.jboss.ejb.plugins.EntityReentranceInterceptor.invoke(EntityReentranceInterceptor.java:71)

at org.jboss.ejb.plugins.EntityInstanceInterceptor.invoke(EntityInstanceInterceptor.java:163)

at org.jboss.ejb.plugins.EntityLockInterceptor.invoke(EntityLockInterceptor.java:89)

at org.jboss.ejb.plugins.EntityCreationInterceptor.invoke(EntityCreationInterceptor.java:54)

at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)

at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267)

at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:128)

at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:118)

at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)

at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)

at org.jboss.ejb.EntityContainer.internalInvoke(EntityContainer.java:489)

at org.jboss.ejb.Container.invoke(Container.java:700)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:324)

at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)

at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)

at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:367)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:324)

at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)

at sun.rmi.transport.Transport$1.run(Transport.java:148)

at java.security.AccessController.doPrivileged(Native Method)

at sun.rmi.transport.Transport.serviceCall(Transport.java:144)

at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)

at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)

at java.lang.Thread.run(Thread.java:534)

3 Respostas

J

Oi

opa opa… “You have an error in your SQL syntax”

Posta aí o código do teu bean, sem o código das interfaces. :joia:

T+

Z

Aí vai os codigos então(lembrando que são beans que usam cmp):

package com.cpqd.dsb.exercise.cmp;

import java.math.BigDecimal;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

import java.util.Random;

import javax.ejb.CreateException;
import javax.ejb.EntityBean;

import org.apache.log4j.Logger;

import com.cpqd.dsb.exercise.InvalidPasswordException;
import com.cpqd.dsb.exercise.ServiceLocator;

import javax.ejb.EntityContext;

import javax.naming.NamingException;

/**
  • @ejb.bean name=“Customercmp”
  • jndi-name=“CustomercmpBean”
  • type=“CMP”
  • primkey-field=“username”
  • schema=“mySchema”
  • cmp-version=“2.x”

*–

  • This is needed for JOnAS.
  • If you are not using JOnAS you can safely remove the tags below.
  • @jonas.bean ejb-name=“Customercmp”
  • jndi-name=“CustomercmpBean”
  • @jonas.jdbc-mapping jndi-name="" jdbc-table-name=“customer”
  • @ejb.persistence
  • table-name=“customer”
  • @ejb.finder
  • query=“SELECT OBJECT(a) FROM mySchema as a”
  • signature=“java.util.Collection findAll()”

*–

  • This is needed for JOnAS.
  • If you are not using JOnAS you can safely remove the tags below.
  • @jonas.finder-method-jdbc-mapping method-name=“findAll”
  • jdbc-where-clause=""
  • @jonas.jdbc-mapping jndi-name=""
  • jdbc-table-name=“customer”
    *@ejb.finder
    *query=“SELECT OBJECT(a) FROM customer a where a.name = ?1”
    signature=“java.util.Collection findByName(java.lang.String username)”
    /
    /
    Classe que executa persistencias e operações com customer no banco de dados
  • extendida por CustomerCMP
    **/

public abstract class CustomercmpBean implements EntityBean {

protected EntityContext eContext;
private ServiceLocator locator;

/**
 * @ejb.interface-method
 *	view-type="local" 
**/
public boolean checkPassword ( String password )throws InvalidPasswordException{ 
	LOGGER.debug("Entering CustomerBean.checkPassword()");
	if (password.equals(getPassword())){
	   LOGGER.debug("Entering CustomerBean.checkPassword()");
	   return true;}
	throw new InvalidPasswordException("Invalid Password");
    }
   

/**
 * @ejb.interface-method
 *	view-type="local" 
 * Executa update de dados no banco de dados
 * @param CustomerTO com os daos atualizados
 * @throws InvalidPasswordException
**/
 public void update(CustomerTO customer, String oldPassword) throws InvalidPasswordException{
	LOGGER.debug("Entering CustomerCMPBean.update()");
	try{
	  this.checkPassword(oldPassword);
	  setName(customer.getName())  ;
	  setPassword(customer.getPassword());
     
   }catch(Exception e){
	LOGGER.debug("UpdateFailed cause : " + e);
	if(e instanceof InvalidPasswordException )
	throw new InvalidPasswordException("UpdateFailed cause :  Invalid password");
			
	}
   finally{
   LOGGER.debug("Leaving CustomerCMPBean.update()");
  } 
}

private static final Logger LOGGER =Logger.getLogger(CustomercmpBean.class);
/**
 * The  ejbCreate method.
 * 
 * @ejb.create-method 
 */
public java.lang.String ejbCreate(CustomerTO customer) throws javax.ejb.CreateException {
	
	LOGGER.debug("Entering CustomercmpCMPBean ejbCreate()");
	this.setName(customer.getName());
	this.setUsername(customer.getUsername());
	this.setPassword(customer.getPassword());
	LOGGER.debug("Leaving CustomercmpCMPBean ejbCreate()"); 
	return null;
}

/**
 * The container invokes this method immediately after it calls ejbCreate.
 * 
 */
public void ejbPostCreate() throws javax.ejb.CreateException {
}

/**
* Returns the username
* @return the username
* 
* @ejb.persistent-field 
* @ejb.persistence
*    column-name="USERNAME"
*     sql-type="VARCHAR"
* @ejb.pk-field 
* @ejb.interface-method
* 
* --
* This is needed for JOnAS.
* If you are not using JOnAS you can safely remove the tags below.
* @jonas.cmp-field-jdbc-mapping  field-name="username"
*	jdbc-field-name="USERNAME"
* 
--
*/
public abstract java.lang.String getUsername();

/**
* Sets the username
* 
* @param java.lang.String the new username value
* 
* @ejb.interface-method
*/
public abstract void setUsername(java.lang.String username);

/**
* Returns the name
* @return the name
* 
* @ejb.persistent-field 
* @ejb.persistence
*    column-name="NAME"
*     sql-type="VARCHAR"
*  
* @ejb.interface-method
* 
* --
* This is needed for JOnAS.
* If you are not using JOnAS you can safely remove the tags below.
* @jonas.cmp-field-jdbc-mapping  field-name="name"
*	jdbc-field-name="NAME"
* 
--
*/
public abstract java.lang.String getName();

/**
* Sets the name
* 
* @param java.lang.String the new name value
* 
* @ejb.interface-method
*/
public abstract void setName(java.lang.String name);

/**
* Returns the password
* @return the password
* 
* @ejb.persistent-field 
* @ejb.persistence
*    column-name="PASSWORD"
*     sql-type="VARCHAR"
*  
* @ejb.interface-method
* 
* --
* This is needed for JOnAS.
* If you are not using JOnAS you can safely remove the tags below.
* @jonas.cmp-field-jdbc-mapping  field-name="password"
*	jdbc-field-name="PASSWORD"
* 
--
*/
public abstract java.lang.String getPassword();

/**
* Sets the password
* 
* @param java.lang.String the new password value
* 
* @ejb.interface-method
*/
public abstract void setPassword(java.lang.String password);

/**
 * @ejb.interface-method
 * tview-type="local"
 * @return CustomerTO
 */
public CustomerTO getCustomerData(){
	LOGGER.debug("Entering CustomerCMPBean.getData()");
	LOGGER.debug("Leaving CustomerCMPBean.getData()");
	return new CustomerTO(getName(), getUsername(), getPassword());
}
/**
 * Sets the entity context
 */
public void setEntityContext(EntityContext context){
	eContext = context;
}
/**
 * Unset the entity context
 */
public void unsetEntityContext(){
	eContext = null;
}

public abstract Collection getOrder();

public abstract void setOrder(Collection newOrder);

/**

  • @ejb.interface-method

  • view-type=local
    
    **/
    
    public void  myOrder() throws NamingException, CreateException{
    
    if (locator == null){
    
    locator = ServiceLocator.getLocator();
    
    }
    
    OrderLocalHome getHome = (OrderLocalHome) locator.getLocalService(OrderLocalHome.JNDI_NAME);
    
    OrderLocal order = getHome.create();
    
    order.setUsername(this.getUsername());
    Collection collection = null;
    collection = this.getOrder();
    collection.add(order);
    this.setOrder(collection);
    

    }

/**

  • @ejb.interface-method
  • view-type=“local”
    **/
    public ArrayList AllOrders(){
    ArrayList list = new ArrayList();
    Iterator c = getOrder().iterator();
    while (c.hasNext()) {
    list.add(
    (OrderLocal)c.next());
    }
    return list;

}
}

package com.cpqd.dsb.exercise.cmp;

import java.math.BigDecimal;

import java.util.Collection;

import java.util.Random;
import javax.ejb.CreateException;

import javax.ejb.EntityBean;

import javax.naming.NamingException;

import com.cpqd.dsb.exercise.ServiceLocator;

/**

  • @ejb.bean name=“Order”
  • jndi-name=“OrderBean”
  • type=“CMP”
  • primkey-field=“orderId”
  • schema=“order”
  • cmp-version=“2.x”

*–

  • This is needed for JOnAS.
  • If you are not using JOnAS you can safely remove the tags below.
  • @jonas.bean ejb-name=“Order”
  • jndi-name=“OrderBean”
  • @jonas.jdbc-mapping jndi-name="" jdbc-table-name=“order”
  • @ejb.persistence
  • table-name=“order”
  • @ejb.finder
  • query=“SELECT OBJECT(a) FROM order as a”
  • signature=“java.util.Collection findAll()”

*–

  • This is needed for JOnAS.
  • If you are not using JOnAS you can safely remove the tags below.
  • @jonas.finder-method-jdbc-mapping method-name=“findAll”
  • jdbc-where-clause=""
  • @jonas.jdbc-mapping jndi-name=""
  • jdbc-table-name=“order”

*–
*
**/

public abstract class OrderBean implements EntityBean {

ServiceLocator locator = null;

/**

* The  ejbCreate method.

*

* @ejb.create-method

*/

public String ejbCreate() throws javax.ejb.CreateException {

// EJB 2.0 spec says return null for CMP ejbCreate methods.

// TODO: YOU MUST INITIALIZE THE FIELDS FOR THE BEAN HERE.

// setMyField(“Something”);

return null;

}
/**
 * The container invokes this method immediately after it calls ejbCreate.
 * 
 */
public void ejbPostCreate() throws javax.ejb.CreateException {
}

/**
* Returns the orderId
* @return the orderId
* 
* @ejb.persistent-field 
* @ejb.persistence
*    column-name="orderId"
*     sql-type="decimal"
* @ejb.pk-field 
* @ejb.interface-method
* 
* --
* This is needed for JOnAS.
* If you are not using JOnAS you can safely remove the tags below.
* @jonas.cmp-field-jdbc-mapping  field-name="orderId"
*	jdbc-field-name="orderId"
* 
--
*/
public abstract String getUsername();

/**
* Sets the orderId
* 
* @param java.math.BigDecimal the new orderId value
* 
* @ejb.interface-method
*/
public abstract void setUsername(String username);

/**
* Returns the orderDate
* @return the orderDate
* 
* @ejb.persistent-field 
* @ejb.persistence
*    column-name="orderDate"
*     sql-type="varchar"
*  
* @ejb.interface-method
* 
* --
* This is needed for JOnAS.
* If you are not using JOnAS you can safely remove the tags below.
* @jonas.cmp-field-jdbc-mapping  field-name="orderDate"
*	jdbc-field-name="orderDate"
* 
--
*/
public abstract java.lang.String getOrderDate();

/**
* Sets the orderDate
* 
* @param java.lang.String the new orderDate value
* 
* @ejb.interface-method
*/
public abstract void setOrderDate(java.lang.String orderDate);

/**
* Returns the adress
* @return the adress
* 
* @ejb.persistent-field 
* @ejb.persistence
*    column-name="adress"
*     sql-type="varchar"
*  
* @ejb.interface-method
* 
* --
* This is needed for JOnAS.
* If you are not using JOnAS you can safely remove the tags below.
* @jonas.cmp-field-jdbc-mapping  field-name="adress"
*	jdbc-field-name="adress"
* 
--
*/
public abstract java.lang.String getAdress();

/**
* Sets the adress
* 
* @param java.lang.String the new adress value
* 
* @ejb.interface-method
*/
public abstract void setAdress(java.lang.String adress);

public abstract Collection getOrderItem();

public abstract void setOrderItem (Collection newOrderItem);

public void myOrderItem() throws NamingException, CreateException{
	if (locator == null){
		locator = ServiceLocator.getLocator();
		}
	OrderItemLocalHome getHome = (OrderItemLocalHome) locator.getLocalService(OrderItemLocalHome.JNDI_NAME);
	OrderItemLocal orderItem = getHome.create();
	Random random = new Random();
	BigDecimal id = new BigDecimal(random.nextInt(100));
	orderItem.setOrderItemId(id);
	Collection collection = null;
	collection = this.getOrderItem();
	collection.add(orderItem);
	this.setOrderItem(collection);
}

}

Z

Bom eu achei o erro, foi só trocar o nome da tabela que funcionou, mas não sei porque…

Se o nome da tabela estivesse errado, ou se ela naum existisse , ele lançaria uma outra excessão…

Vai saber… :roll:

Criado 26 de julho de 2004
Ultima resposta 27 de jul. de 2004
Respostas 3
Participantes 2