Reputation: 131
We use Hibernate and annotations to map our db and entities. But for some data tables I don't want entity classes (Because these table names and all are keep changing) so that the application will be more dynamic
Upvotes: 4
Views: 13622
Reputation: 11
Use Dynamic models introduced in Hibernate 5 version - 5.4.0.Final Hibernate Dynamic Models To achieve this you will need HBM files created.
Session s = openSession();
Transaction tx = s.beginTransaction();
Session s = openSession();
// Create a customer
Map david = new HashMap();
david.put("name", "David");
// Create an organization
Map foobar = new HashMap();
foobar.put("name", "Foobar Inc.");
// Link both
david.put("organization", foobar);
// Save both
s.save("Customer", david);
s.save("Organization", foobar);
tx.commit();
s.close();
Here Customer & Organization are table names Organization is Parent of Customer. Click on the above link for more details
Upvotes: 1
Reputation: 196
- If Persistence class won't be used, then the data encapsulation won't occur thus data can be accessed directly.
- Hibernate Queries interact with the POJO class to fetch data.
- Query, Criteria, HQL all the classes use the POJO for fetching data.
- Hibernate Framework was mainly designed for the ORM Mapping.
- Thus without POJO class, not possible to interact with the database.
Thus using JDBC connection would be the option left.
Upvotes: 1
Reputation: 4017
Use plain JDBC. I'm not sure what you mean by "table names and all are keep changing" but it sounds like a bad idea to me.
What you could do is create the sql query using string concatenation then use plain JDBC to execute it. That way you can keep table names dynamic.
Upvotes: 1
Reputation: 131326
Hibernate provides a way to execute SQL query and to map it to an entity or any class : native sql queries.
Upvotes: 4