Hibernate queries unmapped table

Is there a way to use java bean functionality on unmapped tables?

So, I have a table that is only used for reading and it will never be modified. I need to query it to display the data. But I don’t want hibernate when querying unmapped objects The default Object[] return type. I want to retrieve the result into a collection of custom types. But I have to create an hbm file to perform this operation. Anyway, just create a custom type without the hbm file?

Businessobj method of loading results:

loadResults()
{
String qry = "select col1, col2 from table" ;
List result = (ArrayList) dao.HQLWithTransformer(qry, new CustomTransformer());
}

Custom Transformer:

public class CustomTransformer implements ResultTransformer {



@Override
public Object transformTuple(Object[] rowdata, String[] arg1)
{

return new CustomType(String.valueOf(rowdata[0]),String.valueOf(rowdata[1]));

return null;< br />}

@Override
public List transformList(List arg0) {
return null;
}

}

DAO method:

public Collection HQLWithTransformer(String qry, ResultTransformer rt){ 

List al=null;
try
{
Query q = sess.createQuery(qry);
q.setResultTransformer(rt);
al = (Arr ayList)q.list();
}
catch(HibernateException he)
{
log.debug("Hibernate Exception", he);
}
finally
{
sess.close();
}

return al;
}

You can also use constructor expressions, as shown below:

< pre>List dtos = session.createQuery(“SELECT NEW com.example.MyClass( e.name, e.data) FROM Entity e”).list();

The disadvantage is that you still You must create an entity, a mapping table in order to query it. You can do it with comments, so you don’t have to create an hbm file.

Technically speaking, it is not what you require. I found it to be similar to mapping The query of the report is very useful, so maybe this is what you are looking for.

Is there a way to use the java bean function on the unmapped table?

So, I have a table that is only used for reading and it will never be modified. I need to query it to display the data. But I don’t want hibernate when querying unmapped objects The default Object[] return type. I want to retrieve the result into a collection of custom types. But I have to create an hbm file to perform this operation. Anyway, just create a custom type without the hbm file?

Businessobj method of loading results:

loadResults()
{
String qry = "select col1, col2 from table" ;
List result = (ArrayList) dao.HQLWithTransformer(qry, new CustomTransformer());
}

Custom Transformer:

public class CustomTransformer implements ResultTransformer {



@Override
public Object transformTuple(Object[] rowdata, String[] arg1)
{

return new CustomType(String.valueOf(rowdata[0]),String.valueOf(rowdata[1]));

return null;< br />}

@Override
public List transformList(List arg0) {
return null;
}

}

DAO method:

public Collection HQLWithTransformer(String qry, ResultTransformer rt){ 

List al=null;
try
{
Query q = sess.createQuery(qry);
q.setResultTransformer(rt);
al = (ArrayList) q.list();
}
catch(HibernateException he)
{
log.debug("Hibernate Exception", he);
}
finally
{
sess.close();
}

return al;
}

You can also use constructor expressions as follows:

List dtos = session.createQuery("SELECT NEW com. example.MyClass( e.name, e.data) FROM Entity e").list();

The disadvantage is that you must also create an entity, mapping table in order to query it. You can use comments to complete, So you don’t have to create an hbm file.

Technically speaking, it is not what you asked for. I found it useful for mapping queries like reports, so maybe this is what you are looking for.< /p>

Leave a Comment

Your email address will not be published.