Hello. I want to output data to JSP from MySQL using Hibernate. This is how I output using JDBC and everything worked (this is my tag, then I use it on the jsp page):

private int count; private String nameTable; public void setCount(int count){ this.count = count; } public void setNameTable(String nameTable){ this.nameTable = nameTable; } @Override public int doStartTag() throws JspException { JspWriter out = pageContext.getOut(); DB db = DB.getDB("myhealth_db"); ResultSet rs = db.query("SELECT discription_chapter FROM "+nameTable+"_info WHERE id_article = "+count + " AND id_chapter = 1"); try { if(rs.next()){ out.write(rs.getString("discription_chapter").substring(0,200)); } else { out.write("No Name"); } } catch (SQLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return SKIP_BODY; } 

I need to change everything here under hibernate. as ?

  • 2
    So, as described in the documentation: declare entity classes, set up a session factory, in the doStartTag() method, get a session from the factory, make an HQL query using the session, and loop around the resulting list of entities, displaying the required fields in out . - Sergey Gornostaev
  • I know all this, thanks, but here is the question: ResultSet rs = db.query ("SELECT discription_chapter FROM" + nameTable + "_ info WHERE id_article =" + count + "AND id_chapter = 1"); in rs, I save the necessary data for output from the desired table, in the cycle itself, already on jsp. and what to save in hibernate? maybe there is some example? the factory is set up, HQL query I don’t know which one - Sasha San
  • I changed the code from above, added a couple of fields, I save the names of the tables from the buttons on the site itself, but I don’t understand the HQL query. The count field for the loop on the JSP to output from 1 to 5, the nameTable field for each part from the site that corresponds to the entity. - Sasha San
  • HQL queries return either a single entity object, or a list (List) of entity objects, or a list of object arrays (List <Object []>). Depending on your request. - Sergey Gornostaev
  • Everything is clear, Sergey, thanks, but the question is that I have a lot of tables and I need to display the names of subnames (so to speak) in a loop with this query: SELECT discription_chapter FROM "+ nameTable +" _ info WHERE id_article = "+ count +" AND id_chapter = 1 ; How to replace it with HQL? - Sasha San

1 answer 1

I have no idea about the structure of your data and the required functionality, but this approximate example should suffice as a basis for further independent search for a solution.

Article.java

 import javax.persistence.Entity; import javax.persistence.Column; import javax.persistence.Id; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @Entity public class Article { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String description; public void setId(long id) { this.id = id; } public long getId() { return id; } public void setDescription(String description) { this.description = description; } public String getDescription() { return description; } } 

SomeTag.java

 import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class SomeTag extends TagSupport { private SessionFactory factory; private int count; public SomeTag() { Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistryBuilder srBuilder = new ServiceRegistryBuilder(); srBuilder.applySettings(configuration.getProperties()); ServiceRegistry serviceRegistry = srBuilder.buildServiceRegistry(); factory = configuration.buildSessionFactory(serviceRegistry); } public void setCount(int count){ this.count = count; } @Override public int doStartTag() throws JspException { Session session = factory.openSession(); Query query = session.createQuery("from Article where id = :id"); query.setParameter("id", count); List<Article> articles = query.list(); for (Article article : articles) { out.write(article.getDescription().substring(0, 200)); } session.close(); } } 
  • Sergey, thank you! - Sasha San