How to convert ResultSet object to Hibernate Entity Bean

I have a project, at the beginning of this project we choose Hibernate instead of JDBC for persistence, but we found that Hibernate cannot apply to all cases in this application, so we have to use JDBC to persist in these cases in which Hibernate cannot file application. finally, we choose jooq over JDBC. so there are two persistence technologies in this application i.e. Hibernate & Jooq. now we want to convert the ResultSet created by jooq to Hibernate Entity Beans. I have searched for information about Hibernate but found nothing. It seems that Hibernate doesn't have an API for this.

So I have a few questions!

  • How can I get the current Hibernate context?
  • How do I convert a ResultSet object to a Hibernate Entity Bean using this context (see question 1)?

Thanks everyone.

+3


source to share


2 answers


Do you have a look at jOOQ Record.into () and ResultQuery.fetchInto () Methods ? They support displaying jOOQ entries for custom POJOs. You can either generate these POJOs with jOOQ directly or define them yourself. If JPA annotations are present on your POJO, they will be used. Otherwise jOOQ maps entries to your POJO based on method naming conventions.

Example:

@Entity
public class POJO {
  private int id;

  @Column(name = "ID")
  public void setId(int id) {
    this.id = id;
  }

  public int getId() {
    return id;
  }
}

// Fetch several of the above POJO into a list
List<POJO> result = 
DSL.using(connection, dialect)
   .select(MY_TABLE.ID)
   .from(MY_TABLE)
   .fetchInto(POJO.class);

// Fetch single POJO's
POJO pojo = 
DSL.using(connection, dialect)
   .select(MY_TABLE.ID)
   .from(MY_TABLE)
   .where(MY_TABLE.ID.equal(1))
   .fetchOne()
   .into(POJO.class);

      



Now in the above example, you will be using jOOQ to execute queries. Since you are talking about converting objects ResultSet

to your custom POJOs, I am assuming you are only using jOOQ as a query builder. Perhaps in this case, you can still use jOOQ to fetch data from JDBCResultSet

like this:

ResultSet rs = // [ your querying here ]
List<POJO> result = create.fetch(rs).into(POJO.class);

      

+3


source


Since you decided to use JDBC

, you need to code what hibernate does for you. This means you need to write code to read from ResultSet

and populate the Beans.

how



MyBean mb = new MyBean();
mb.setValue1(resultSet.get...);

      

Now, forget the above.If you are at the start of a project, I highly recommend considering Hibernate. Start hibernation here .

0


source







All Articles