join - One-to-many mapping in hibernate -
my levelterm.hbm.xml
file is:
<hibernate-mapping> <class name="com.entity.levelterm" table="level_term" catalog="test"> <id name="levelid" type="java.lang.integer"> <column name="level_id" /> <generator class="identity" /> </id> <property name="level" type="int"> <column name="level" not-null="true" /> </property> <property name="term" type="int"> <column name="term" not-null="true" /> </property> <property name="session" type="int"> <column name="session" not-null="true" /> </property> <list name="list_course"> <key column="level_id"/> <one-to-many column="course_code" class="com.entity.course"/> </list> </class> </hibernate-mapping>
and levelterm
class is:
@entity public class levelterm implements java.io.serializable { @id @generatedvalue(strategy= generationtype.auto) private integer levelid; private int level; private int term; private int session; @onetomany private list<course>list_course; public list<course> getlist_course() { return list_course; } public void setlist_course(list<course> list_course) { this.list_course = list_course; } public list<student> getlist_student() { return list_student; } public void setlist_student(list<student> list_student) { this.list_student = list_student; } public levelterm() { } public levelterm(int level, int term, int session) { this.level = level; this.term = term; this.session = session; } public integer getlevelid() { return this.levelid; } public void setlevelid(integer levelid) { this.levelid = levelid; } public int getlevel() { return this.level; } public void setlevel(int level) { this.level = level; } public int getterm() { return this.term; } public void setterm(int term) { this.term = term; } public int getsession() { return this.session; } public void setsession(int session) { this.session = session; }
}
and hibernate.cfg.xml
configuration file is:
<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.mysqldialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.current_session_context_class">thread </property> <property name="hibernate.hbm2ddl.auto">create</property> <mapping resource="com.entity/student.hbm.xml"/> <mapping resource="com/entity/address.hbm.xml"/> <mapping resource="com.entity/course.hbm.xml"/> <mapping resource="com.entity/levelterm.hbm.xml"/> </session-factory> </hibernate-configuration>
my code supposed create join table "level_term_list_course" in mysql database. no table created.
no, mapping not supposed generate join-table. looks uni-directional one-to-many association, , managed foreign-key on many
side (the courses in example). take @ this example , associated explanations. de facto sql
way of having one-to-many
association.
also, if need join table, might need akin this example in hibernate docs. basically, if need join-table in uni-directional one-to-many
assocation, use many-to-many
element unique
attribute set true
; makes association one-to-many
.
Comments
Post a Comment