Hi All
I'm totally new to Hibernate.
I´ve an application that will store and retrieve some simple data from a DB (MySQL, by now...) and I wonder if Hbiernate could be needed/useful for me.
The code where my application will store/retrieve data is a Bean then persistence is kept and Hibernate will not help me at this point.
After read docs I think the only way that Hibernate could help me is the use of his own SQL language to allow me to change DB easily in the future but as far as I know my querys are so simple, I'm not sure if it's really needed to learn Hibernate and to place a lot of extra code for this.
Am I right or wrong ? Could Hibernate make more than I expresed above ?
My code is something like this:
------------------
Code:
public class RDBMTicketRegistry implements TicketRegistry {
private Connection con = getConnection();
/** The Commons Logging instance. */
private final Log log = LogFactory.getLog(getClass());
/**
* Add a ticket to the DB
*/
public void addTicket(final Ticket ticket) {
if (log.isDebugEnabled())
log.debug("Entre en addTicket");
if (ticket == null) {
throw new IllegalArgumentException("ticket cannot be null");
}
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(ticket);
// serialize the employee object into a byte array
byte[] ticketAsBytes = baos.toByteArray();
PreparedStatement pstmt = con.prepareStatement
("INSERT INTO tickets VALUES(?, ?)");
ByteArrayInputStream bais =
new ByteArrayInputStream(ticketAsBytes);
// bind our byte array to the emp column
pstmt.setString(1, ticket.getId());
pstmt.setBinaryStream(2,bais, ticketAsBytes.length);
pstmt.executeUpdate();
pstmt.close();
} catch(IOException ioe) {
log.error("Error IOE al insertar ticket id " + ticket.getId() + " exception: " + ioe.getMessage());
} catch(SQLException sqle) {
log.error("Error SQL al insertar ticket id " + ticket.getId() + " exception: " + sqle.getMessage());
}
}
/**
*
* @return
*/
private Connection getConnection() {
Connection c = null;
System.out.println("Entre en getConnection");
try {
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource) envCtx.lookup("jdbc/MySQLDB");
// Allocate and use a connection from the pool
c = ds.getConnection();
} catch(Exception e) {
log.error("Excepcion al conectar a Db " + e.getMessage());
}
if (c != null) {
System.out.println("Connection a DB es Ok");
}
return c;
}
}
And there are mnethods to getTickets and deleteTickets in the same way.
Thanks in advance
J
[/code]