View Full Version : Looking for Hibernate - Spring examples
theone
Aug 19th, 2004, 06:37 PM
Hi,
I'm new to both Hibernate and Spring Framework. I've been reading and have put together a web app using Spring Framework. Now I would like to add Hibernate functionality into the web app. I'm not sure how to go about doing it. I want to know how to add hibernate bean references to myweb-servlet.xml. Can some one show me some examples and what are some better ways of doing this?
I used Hibernate Synchronizer to create my mapping files and DAO's. I don't know exactly how to wire them into Spring Framework. Is there a better tool for generating the mapping files and DAO's and wire them together automatically?
Thank you
feester
Aug 19th, 2004, 09:42 PM
add hibernate bean references to myweb-servlet.xml
I suggest you put persistence related bean definitions in separate xml file like contextPersistence.xml. In web.xml, include all xml files pertinent to your environment in a <context-param> with name 'contextConfigLocation'. For example:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/contextPersistence.xml
,/WEB-INF/contextControllers.xml</param-value>
</context-param>
some examples and what are some better ways of doing this
In spring distribution, there is /sample/petclinic application that uses either hibernate or jdbc.
smccrory
Aug 19th, 2004, 10:09 PM
I highly recommend Matt Raible's Spring Live (http://jroller.com/page/raible) e-book, especially Chapter 2 (http://www.theserverside.com/articles/content/SpringLive_Chapter/chapter2.pdf). Helped me get started with both Spring and Hibernate in very short order.
Scott
toddmcgrath
Apr 28th, 2006, 01:04 PM
Here's documentation and code of using Hibernate Synchronizer 3.1, Hibernate 3 and Spring:
Hibernate Synchronizer 3.1, Hibernate 3 and Spring Example (http://www.supergloo.com/blog/index.php/2005/11/10/using-hibernate-synchronizer-3-with-spring-and-hibernate-3)
Hope this helps. There are other posts about using different versions of Hibernate Sync and Hibernate as well.
Costin Leau
May 1st, 2006, 04:48 PM
In case you were not aware, Spring distribution comes with a set of samples which work on Hibernate and other persistence technologies. They are good practice for beginners as they contain common patterns which are independent of IDEs or plugin.
lekshmi.giridhar
Jul 11th, 2008, 02:14 AM
I have created a "Customer" table which has Name,id and phone
I have an embeddable class "Name" having firstname and lastname.
Code is as follows..
Customerbean.java
====================
@Entity
@Table(name="Customer")
public class CustomerRegistrationBean {
private CustomerName CustomerName;
private long phone;
private String customerid;
public CustomerRegistrationBean()
{
}
public CustomerRegistrationBean(String customerid,String firstname, String lastname, long phone)
{
this.customerid = customerid;
this.CustomerName = new CustomerName(firstname, lastname);
this.phone = phone;
}
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "firstname", column = @Column(name = "firstname")),
@AttributeOverride(name = "lastname", column = @Column(name = "lastname"))
})
public CustomerName getCustomerName() {
return CustomerName;
}
public void setCustomerName(CustomerName CustomerName) {
this.CustomerName = CustomerName;
}
@Column(name="phone")
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
@Id
@Column(name="customerid" )
public String getCustomerid() {
return customerid;
}
public void setCustomerid(String customerid) {
this.customerid = customerid;
}
}
Name.java
++++++++++++++++==
@Embeddable
public class CustomerName implements Serializable
{
private static final long serialVersionUID = 4209558006137354948L;
private String firstname;
private String lastname;
public CustomerName()
{
}
public CustomerName(String firstname, String lastname)
{
this.firstname = firstname;
this.lastname = lastname;
}
@Column(name="firstname")
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@Column(name="lastname")
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
================================================== ==========================
When I run this it is giving an exception
org.apache.jasper.JasperException: Invalid property 'firstname' of bean class [com.palnar.library.service.CustomerRegistrationBea n]: Bean property 'firstname' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
Please help me in this.....
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.