This content has been marked as final.
Show 3 replies
-
1. Re: Does Hibernate 3.2 support generating Database schema fr
andydale Nov 17, 2006 3:30 AM (in response to legolas.w)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 definitionCREATE 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
legolas.w Nov 17, 2006 11:08 AM (in response to legolas.w)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 Nov 17, 2006 11:16 AM (in response to legolas.w)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