Es posible que encuentres alguna base de datos que no tenga las tablas relacionadas y que además no puedes tocar, en ese caso mejor olvidarse de hibernate y utilizar consultas SQL a la antigua usanza.
Este es un ejemplo de como pasar los resultados de una consulta a la vista que los mostrará
package DB
import grails.converters.JSON;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import groovy.sql.Sql
import groovy.sql.GroovyRowResult
import org.springframework.dao.DataIntegrityViolationException
import org.springframework.security.access.annotation.Secured
import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional
import org.hibernate.persister.entity.AbstractEntityPersister
import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder
@Transactional
class DdataService {
boolean transactional = true
def dataSource
def recover(params, nameClass, principalTable, nonRelational) {
def sql = new Sql(dataSource)
def sqlProvider = new Sql(dataSource)
def rows = []
def data = [:]
data['page']=params.page
def sortname = params.sortname
def sortorder = params.sortorder
//def all=params.all
//def all2=params.all2
def cadSql1, cadSql2, cadSql
def numRegister = (Integer.valueOf(params.page)-1)*(Integer.valueOf(params.rp))
def nPages, nReg
def GrailsDomainBinder = getClass().classLoader.loadClass('org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder')
def cLs = GrailsDomainBinder.newInstance().getMapping(Class.forName(nameClass))?.columns
def nReg7='select count(*) as nreg from '+principalTable+' where ('
def cadSql7='select * from '+principalTable+' where ('
def tableNonRelational
def fieldNonRelational
nonRelational.each { entry ->
fieldNonRelational=entry.key
tableNonRelational=entry.value
}
def control = false
cLs.each{ k, v ->
if(control==true){
if(!v?.columns?.name.get(0).equals(fieldNonRelational)){
nReg7=nReg7+" OR "+v?.columns?.name.get(0)+" LIKE '%"+all+"%'"
cadSql7=cadSql7+" OR "+v?.columns?.name.get(0)+" LIKE '%"+all+"%'"
}
}
if(control==false){
if(!v?.columns?.name.get(0).equals(fieldNonRelational)){
nReg7=nReg7+v?.columns?.name.get(0)+" LIKE '%"+all+"%'"
cadSql7=cadSql7+v?.columns?.name.get(0)+" LIKE '%"+all+"%'"
}
control=true
}
}
//dejo por construir las select
nReg='select count(*) as nreg from ....
cadSql='select * from ...
sqlProvider.eachRow(nReg){prov ->
nPages=prov.nreg
}
data['total']=nPages
def pr = [:]
sqlProvider.eachRow('select * from '+tableNonRelational){prov ->
pr.put(prov.id,(prov.(fieldNonRelational.toString())))
}
sql.eachRow(cadSql) { is ->
def value =[:]
cLs.each{ k, v ->
try {
if(k==fieldNonRelational){
value.put(k,pr[Integer.valueOf(is.(v?.columns?.name.get(0)))])
}else{
value.put(k,is.(v?.columns?.name.get(0)))
}
} catch (NullPointerException e){
value.put(k,'');
} catch(NumberFormatException e){
value.put(k,'');
}
}
rows << [cell:value]
}
data['rows']=rows
[data:data]
}
}
Post protegido por post2i.es
No hay comentarios:
Publicar un comentario