So by ID, one way is with criteria:
<% // Begin transaction as normal Session s = HibernateUtil.getSessionFactory().getCurrentSession(); s.beginTransaction(); CodeSnipet snipet = new CodeSnipet(); // new object holder Criteria c = s.createCriteria(CodeSnipet.class); c.addOrder(Order.asc("idcodesnipets")).setMaxResults(1); //only want one. List l = c.list(); snipet = (CodeSnipet)l.get(0); // just want the first one in the list %> <p>Here is a snipet:</p> <p>Title: <% out.println(snipet.getCodetitle()); %> </p> <p>File Name: <% out.println(snipet.getCodefilename()); %> </p> <p>Code: <% out.println(snipet.getCodetext()); %> </p>
Imports:
<%@page import="java.util.List"%> <%@page import="org.hibernate.criterion.Order"%> <%@page import="org.hibernate.Criteria"%>
By the way, I had the wrong dialect set from some stupid copy pasta, so if you get a 'SQL Grammar Error', check that if you are using MySQL you have this:
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
Set as your dialect in your hibernate config file. So it will look like:
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
No comments:
Post a Comment