3 Replies Latest reply on Nov 17, 2006 11:16 AM by andydale

    Does Hibernate 3.2 support generating Database schema from a

      Hi
      Thank you for reading my post
      I am reading pro Hibernate 3.0 and it says that Hibernate 3 does not support generating database schema from the annotated java classes.
      is it supported in Hibernate 3.2 ?
      if yes, how i can do it?

      thanks

        • 1. Re: Does Hibernate 3.2 support generating Database schema fr
          andydale

          Yes Hibernate can create the DB schema from annotated classes, consider the following class

          @Entity
          @Table(name = "Student")
          @SequenceGenerator(name = "Student_Sequence", initialValue = 1, allocationSize = 1, sequenceName = "student_seq")
          
          public final class Student {
           //************** class variables *****************
           /**
           * The ID of the entity.
           */
           private int mId;
          
           /**
           * Name of the student.
           */
           private String mName;
          
          
           /**
           * Course student is enrolled on
           */
           private Collection<Course> mCourses = new ArrayList<Course>();
           //************************************************
          
           //*********** getters and setters ****************
          
           /**
           * Return the courses the student is enrolled on.
           * @return mCourses courses
           */
           @ManyToMany(mappedBy = "students", fetch = FetchType.EAGER, cascade ={CascadeType.PERSIST, CascadeType.MERGE})
           public Collection<Course> getCourses() {
           return mCourses;
           }
          
           /**
           * See the course the student is enrolled on
           * @param pCoursesEnrolledOn courses
           */
           public void setCourses(final Collection<Course> pCourses) {
           mCourses = pCourses;
           }
          
           /**
           * get the Id.
           * @return mId id
           */
           @Id
           @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Student_Sequence")
           @Column(name = "student_id")
           public int getId() {
           return mId;
           }
          
           /**
           * Set the id.
           * @param pId id
           */
           public void setId(final int pId) {
           this.mId = pId;
           }
          
           /**
           * Return the name.
           * @return mName name
           */
           public String getName() {
           return mName;
           }
          
           /**
           * set the name.
           * @param pName name
           */
           public void setName(String pName) {
           this.mName = pName;
           }
           //************************************************
          }

          will create a table on deploy with the definition
          CREATE TABLE student
          (
           student_id int4 NOT NULL,
           name varchar(255),
           CONSTRAINT student_pkey PRIMARY KEY (student_id)
          )


          The above example is pretty simple, but you could just deploy a class annotated with @Entity. On deploy JBoss/Hibernate would convert it into a DB table in the DB specified in a persistence.xml file (must be in META-INF of ejb.jar).

          Experiment a bit, and possibly buy yourself the Enterprise JavaBeans 3.0 book (Bill Burke, Richard Monson-Haefel), as it gives you some decent examples of how EJB3 works.


          Cheers,

          Andy


          • 2. Re: Does Hibernate 3.2 support generating Database schema fr

            Hi
            Thank you for your reply.
            I have already a book named Pro hibernate 3.0, In that book author said that hibernate 3.0 does not support generating schema from annotated class.
            and it just generate database from mapping files.

            Now i learned that a ANT task helps to generate schema from mapping files.
            what i want to know is about ant task or a solution for generating schema from annotated classes.

            So, is there any ant task for this?
            Thanks

            • 3. Re: Does Hibernate 3.2 support generating Database schema fr
              andydale

              Hi,

              I do not know if there is an Ant task for this, i just let JBoss do it for me on deployment of my .ear file.

              Andy