I have 2 tables in my database
table1--> id, name, address
table2--> id, tb1id
How can I get the table2 information using hibernate framework?
you can use ResultsetMetaData in spring/hibernate.
Check this answer..
Use annotations for getting ResultsetMetaData.
Thanks..
Related
I have a ResultSetMetaDataobject.
PreparedStatement ps=con.prepareStatement("select var1, var2 from test1, test2");
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("Total columns: "+rsmd.getColumnCount());
System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));
System.out.println("Column Type Name of 1st column: "+rsmd.getColumnTypeName(1));
I need to create an new empty table with all the fields in the above ResultSetMetaData
Open a new connection in different database
Use the meta data above
Create a new empty table with this meta data
I can see two possible solutions
Reflection. In general to create a table with Hibernate, I need to create a bean. But I don't know in advance what fields the ResultSetMetaData will contain . Should I create this bean with reflection?
HQL CREATE TABLE . Is this possible?
What is the simplest way to do this? How can I do this?
Couple of solutions that I can think of:
SchemaExport - Iterate through your ResultSetMetaData which exposes API's like getColumnName and getColumnType to get the information you need. Using this information create a hibernate mapping XML and send it to SchemaExport.create.
// yourClassMapper.xml generated in runtime
Configuration config = new Configuration();
config.addResource("yourClassMapper.xml");
new SchemaExport(config).create(true,true)
Native Query - As Usual Iterate through your metadata to figure out what should the columns be.
Using createSQLQuery or even using createNativeQuery
session.createSQLQuery("create table .....").executeUpdate();
This link provides an explanation on how to create custom fields. You can make use of its DOM Parser for 1st approach or create just tables and use the method mentioned in this blog to create custom fields. (Refer to this class MappingManager and its related custom map configuration)
Lastly, You need 2 different datasource to be configured for this. And inject the second database's source for creating session/sessionfactory in above implementations.
I dont think you need to create bean for it. If you want to create new table with same fields then simple fire this query on database using hibernate or jdbc.
SHOW CREATE TABLE tablename
Now simply change the name of the table in query, can be easily done in Java.
Now you does not need to create any bean for newly created table.
There are two ways for fetching data from table in hibernate. First way is get table data in form of bean object, another way is fire sql query and get data in form of Object (Java Class). you can get data in form of Object and Object Array using hibernate.
When we use join query in hibernate, then there is no specific type of bean is available for handling result of join because it contain column from multiple table so you will get data in form of Object and Object Array.
Just get Data in form of Object or Object Array.
Check This Link of my github... not exactly what you want but still will help :
here
I hope I Helped you.
I have multiple dao classes, I want to replace them with Hibernate ORM. I have generated all the POJOs and mapping for each table. Now, I don't know how to move ahead. Can someone help ? Thanks in advance.
You don't have to replace the SQL queries, just setup the hibernate configuration & use the createSQLQuery method
String sql = "SELECT * FROM EMPLOYEE";
SQLQuery query = session.createSQLQuery(sql);
I've three entity class with no mapped relations whatsoever either with annotations or in xml.
FILE_ACC_DETAILS:[REF_NO, NAME, TYPE,........];
FILE_DETAILS: [REF_NO, BATCH_NO, FINCL_YEAR,........];
FILE_STATUS: [BATCH_NO, STATUS, HASH, SEQ_NUM,........];
I want to do this query using the criteria api of hibernate:
SELECT acc.REF_NUM,acc.NAME,detail.BATCH_NO, detail.FINCL_YEAR, status.STATUS
FROM FILE_ACC_DETAILS as acc
INNER JOIN FILE_DETAILS as detail ON acc.REF_NO=detail.REF_NO
INNER JOIN FILE_STATUS as status ON detail.BATCH_NO=status.BATCH_NO
Please consider that the common columns name maybe different in actual scenario. If not complete answer, conceptual explanation and partial example would be appreciated. Pls ignore if any syntax error in here. Thanks in advance.
I have been using Hibernate 3.2 intailly for my J2EE application with Spring 2.5.Recently I wanted a feature of hibernate 3.5(BigInt Identity support).So I have upgraded my hibernate and now I facing a different issue with my queries.
HQL Query:-
select table from tableVO table where tableVO.subTableVO.id=:tableVO.id
SQL Query:-
select table_1_ID from table cross join subTable where subTable.id =table.id
I see that cross join is being done by hibernate which is not accepted by Sybase ASE. How can I fix this?
Check the dialect you have set in hibernate configuration. I'm going to assume you're running on Sybase ASE 15.x. As you found out, Sybase does not (yet) support CROSS JOIN, which is what the SybaseDialect attempts to use. Instead, use SybaseASE157Dialect or SybaseASE15Dialect. It will generate syntax that should look like:
select table_1_ID from table, subTable where subTable.id =table.id
You can change hibernate dialect,
in hibernate.cfg
<property name="hibernate.dialect">com.YourProject.YourDialect</property>
in your dialect class you should enter the syntax you want executed.
example dialect change for DB2
public class DB2390Dialect extends DB2Dialect
{
public String getIdentitySelectString() {
return "select identity_val_local() from sysibm.sysdummy1";
}...
}
Hope this helps
This is a bug with implicit joins in Hibernate. You can fix it by aliasing your joins:
select table from tableVO table
join tableVO.subtableVO subtable
where subtable.id=:tableVO.id
currently I am using the following to pull rows from a table called Table:
return getHibernateTemplate().find("from Table");
How do I use hibernate to pull only the first n rows from the table (i.e. like a mySql limit would do)?
Thanks.
Update: so is this the proper way to do it?
getHibernateTemplate().setMaxResults(35);
return getHibernateTemplate().find("from Table");
Use HIbernateTemplate setMaxResults to limit the results.
I ended up using a Hibernate Criteria query to do this and it works properly. I made use of the setFirstResult() and setLastResults() methods.