It's unclear to me what you're using this for (I don't understand what codigo
or generos mean in portuguese), but if you're using this method to generate
primary key values, it's better to let Cayenne do this for you
automatically, or at least use your database's built-in method to do it.
Using a select(max) function is not guaranteed to return a unique value.
See the following link for more information.
http://www.objectstyle.org/cayenne/userguide/design/autopk.html
-Mike
Reinaldo Coelho Sartorelli <reinaldo_sartorell..ahoo.com.br> wrote:
>
> //Query da Seleção.
> String sql = "SELECT MAX(generos.COD_GENERO) AS codigo FROM generos";
> SQLTemplate query = new SQLTemplate(Generos.class, sql, true);
> query.setFetchingDataRows(true);
> List lst = conexao.performQuery(query);
> if(!lst.isEmpty()) {
> //Gera mapa de retorno.
> Map a = (Map) lst.get(0);
> //Verifica se existe algum registro atualmente.
> if (a.get("codigo") == null) {
> return 1;
> }
> else{
> int i = Integer.parseInt(a.get("codigo").toString());
> return ++i;
> }
> }
> else{
> return 1;
> }
> }
> catch(Exception e){
> System.out.println("Erro:" + e.getMessage());
> //Em caso de erro, retorna 0.
> return 0;
> }
This archive was generated by hypermail 2.0.0 : Mon Aug 02 2004 - 10:41:49 EDT