hibernate中的一对多映射

时间:2013-08-23 15:40:47

标签: hibernate join hibernate-mapping

我的LevelTerm.hbm.xml文件是:

 <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>

我的LevelTerm课程是:

@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;
}

}

hibernate.cfg.xml配置文件是:

<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>

我的代码应该在MySQL数据库中创建一个连接表“LEVEL_TERM_LIST_COURSE”。但是没有创建表格。

1 个答案:

答案 0 :(得分:0)

不,您的映射不应该生成连接表。它看起来像是一个单向的一对多关联,它将由many侧的外键(您的示例中的课程)进行管理。请查看this example及相关说明。这是SQL关联的事实one-to-many方式。

此外,如果您需要连接表,则可能需要在hibernate文档中执行类似于this example的操作。基本上,如果您需要单向one-to-many关联中的联接表,则使用many-to-many元素并将unique属性设置为true;这有效地促成了关联one-to-many