- This topic has 7 replies, 2 voices, and was last updated 19 years, 3 months ago by pcleung.
-
AuthorPosts
-
pcleungMemberI 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?
Riyad KallaMemberI 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 😉
pcleungMemberBut 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
Riyad KallaMemberIf 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).
pcleungMemberThank 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 requestCan 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();
Riyad KallaMemberCan 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
pcleungMemberAnything 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.
pcleungMemberAfter configuring my tomcat jndi, it can retrieve records from mysql database eventually.
Thank you very much. -
AuthorPosts