[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Re: Does Hibernate 3.2 support generating Database schema fr
andydale
do-not-reply at jboss.com
Fri Nov 17 03:30:33 EST 2006
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
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3986761#3986761
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3986761
More information about the jboss-user
mailing list