Olá,
já criei alguns aplicativos web utilizando Spring. Porém, estou tendo problemas para criar minha primeira aplicação desktop. Ao tentar gravar um registro na tabela, recebo um erro que não consigo entender. Pelo console, percebe-se que o Spring carregou o applicationContext.xml.
Ao tentar salvar o objeto no banco, recebo o erro “org.springframework.dao.InvalidDataAccessApiUsageException: Table name is required”
Pelo debug, vi que a referência a este objeto realmente está null na referência ao schema, mas não sei o que alterar para que funcione, por isso peço ajuda aos colegas.
Seguem os trechos do código
- applicationContext.xml
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/teste</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
<bean id="teste1"
class="teste.Teste1">
<property name="clienteDao" ref="clienteDao" />
</bean>
<bean id="cliente"
class="br.org.pnsfgo.bean.Cliente">
</bean>
<bean id="clienteDao"
class="br.org.pnsfgo.dao.ClienteDAO">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
</beans>
- ClienteDAO
public class ClienteDAO extends NamedParameterJdbcDaoSupport{
private SimpleJdbcInsert sji;
(...)
public int criar(Cliente c){
sji = new SimpleJdbcInsert(getJdbcTemplate());
sji.withTableName("CLIENTE");
sji.usingGeneratedKeyColumns("CLIENTE_ID");
Map<String, Object> args = new HashMap<String, Object>();
args.put(...)
.
.
.
getJdbcTemplate().executeAndReturnKey(args).intValue();
}
- Teste1
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Cliente c1 = new Cliente(.....);
clienteDao.criar(c1); <<< erro!!
.
.
.
}
- mensagens no console
17/10/2011 10:37:06 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b6e39f: display name [org.springframework.context.support.ClassPathXmlApplicationContext@b6e39f]; startup date [Mon Oct 17 10:37:06 BRST 2011]; root of context hierarchy
17/10/2011 10:37:07 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
17/10/2011 10:37:07 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@b6e39f]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1295fe8
17/10/2011 10:37:07 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1295fe8: defining beans [dataSource,jdbcTemplate,cliente,teste1,clienteDao]; root of factory hierarchy
17/10/2011 10:37:07 org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Table name is required
at org.springframework.jdbc.core.simple.AbstractJdbcInsert.compile(AbstractJdbcInsert.java:230)
at org.springframework.jdbc.core.simple.AbstractJdbcInsert.checkCompiled(AbstractJdbcInsert.java:291)
at org.springframework.jdbc.core.simple.AbstractJdbcInsert.doExecuteAndReturnKey(AbstractJdbcInsert.java:353)
at org.springframework.jdbc.core.simple.SimpleJdbcInsert.executeAndReturnKey(SimpleJdbcInsert.java:106)
at br.org.pnsfgo.dao.ClienteDAO.criar(ClienteDAO.java:54)
at teste.Teste1.main(Teste1.java:36)