Announcement Announcement Module
Collapse
No announcement yet.
Looking for Hibernate - Spring examples Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Looking for Hibernate - Spring examples

    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

  • #2
    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:
    Code:
      <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.

    Comment


    • #3
      I highly recommend Matt Raible's Spring Live (http://jroller.com/page/raible) e-book, especially Chapter 2 (http://www.theserverside.com/article...r/chapter2.pdf). Helped me get started with both Spring and Hibernate in very short order.

      Scott

      Comment


      • #4
        Here's documentation and code of using Hibernate Synchronizer 3.1, Hibernate 3 and Spring:

        Hibernate Synchronizer 3.1, Hibernate 3 and Spring Example

        Hope this helps. There are other posts about using different versions of Hibernate Sync and Hibernate as well.

        Comment


        • #5
          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.

          Comment


          • #6
            Hibernate @Embeddable annotations

            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..
            Code:
            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;
            }
            }
            Code:
            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.....

            Comment


            • #7
              The one that comes with Spring is pretty good. If you are using eclipse and want to see all the steps on configuration and running the application this Spring and Hibernate Tutorial would be helpful. It has step by step instruction with screen shots on how to configure eclipse with hibernate and JPA to create your first application. Instead of JPA , you can use hibernate also.
              Last edited by vinay076; Dec 4th, 2009, 02:51 PM.

              Comment


              • #8
                Costin,

                Do you have a URL for the "Spring distribution samples" you mentioned? This site has gotten so big that it is hard to find stuff.

                Jerry

                Comment

                Working...
                X