facebook

Simple Hibernate question

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #237073 Reply

    pcleung
    Member

    I use MyEclipse 4.0

    For Hibernate mapping generation,
    the ID generator has the option of “native”
    and there are “Java Type” and “Hibernate Type”.

    Which type should I use? Java or Hibernate
    Should the table have a column of Id with data type Long
    so that the ID generator “native” becomes valid?

    #237089 Reply

    Riyad Kalla
    Member

    I always suggest making the column of type “autoincrement” in the database, then use type Native. The database knows how to increment it’s keys quite well, so let it do it 😉

    #237098 Reply

    pcleung
    Member

    But the table is user profile. Username is already unique.
    If the table does not an autoincrement field, will Hibernate function properly?
    If the table does not have such field, can I choose type instead of “Native”?

    BTW, what is the different of creation mapping between Java Type and Hibernate Type?

    Thank you

    #237103 Reply

    Riyad Kalla
    Member

    If the table does not an autoincrement field, will Hibernate function properly?

    Yes that is fine.

    If the table does not have such field, can I choose type instead of “Native”?

    Choose native, your username field is unique, if you have enforced this by making the column UNIQUE, then this *is* native. All “native” means it that “let the DB worry about uniqueness”, in this case you are letting the DB handle the uniqueness constraint.

    BTW, what is the different of creation mapping between Java Type and Hibernate Type?

    It is covered in the Hibernate docs, Java Type is stuff like java.lang.Interger, String, etc., HibernateType pertains to the instances of the wrapping Hibernate column types (they have their own specialty classes).

    #237107 Reply

    pcleung
    Member

    Thank you rkalla,

    I code my testApp as I read in Hibernate demo.

    But my jsp runs into error as follows.
    javax.servlet.ServletException: Cannot find bean userProfileList in scope request

    Can you see any obvious errors?

    My jsp like this
    <%@ page import=”com.erp.hibernate.UserProfileService” %>
    <%@ page import=”java.util.List” %>

    <%
    List userProfileList = UserProfileService.getInstance().getUserProfileList();
    request.setAttribute(“UserProfile”, userProfileList);
    %>
    <table>
    <logic:iterate id=”element” name=”userProfileList” scope=”request” type=”com.erp.hibernate.UserProfile”>
    <tr>
    <td><bean:write name=”element” property=”usernm” /></td>
    <td><bean:write name=”element” property=”passwd” /></td>
    </tr>
    </logic:iterate>
    </table>

    My UserProfileService.java is
    Query query =
    session.createQuery(
    “select UserProfile from com.erp.hibernate.UserProfile UserProfile order by UserProfile.usernm”);
    return query.list();

    #237111 Reply

    Riyad Kalla
    Member

    Can you see any obvious errors?

    Yes 🙂

    
    request.setAttribute("UserProfile", userProfileList); 
    

    Here you put a variable named “userProfileList” into your request scope under the key “UserProfile”

    
    <logic:iterate id="element" name="userProfileList" scope="request" type="com.erp.hibernate.UserProfile"> 
    

    Here you are trying to access your userProfileList variable, but you are not using the key “UserProfile” as you should, you are using the name of the variable. You need to use the key you used to place it into the request scope: UserProfile

    #237147 Reply

    pcleung
    Member

    Anything to do with my hibernate.cfg.xml or UserProfile.hbm.xml?

    The error becomes the following.

    exception

    org.apache.jasper.JasperException: org.hibernate.HibernateException: Not able to obtain connection
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    root cause

    java.lang.RuntimeException: org.hibernate.HibernateException: Not able to obtain connection
    com.erp.hibernate.UserProfileService.getUserProfileList(UserProfileService.java:182)
    org.apache.jsp.user.addUserProfile_jsp._jspService(org.apache.jsp.user.addUserProfile_jsp:108)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    My hibernate.cfg.xml:

    <property name=”myeclipse.connection.profile”>mysql</property>
    <property name=”connection.url”>
    jdbc:mysql://localhost:3306/erp
    </property>
    <property name=”connection.username”>xxxt</property>
    <property name=”connection.password”>xxx</property>
    <property name=”connection.driver_class”>
    com.mysql.jdbc.Driver
    </property>
    <property name=”dialect”>
    org.hibernate.dialect.MySQLDialect
    </property><mapping resource=”com/erp/hibernate/UserProfile.hbm.xml” />

    SessionFactory.java:
    private static String CONFIG_FILE_LOCATION = “/hibernate.cfg.xml”;

    My hibernate.cfg.xml resides under /src directory.

    #237148 Reply

    pcleung
    Member

    After configuring my tomcat jndi, it can retrieve records from mysql database eventually.
    Thank you very much.

Viewing 8 posts - 1 through 8 (of 8 total)
Reply To: Simple Hibernate question

You must be logged in to post in the forum log in