0

我的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”。但是没有创建表。

4

1 回答 1

0

不,您的映射不应该生成连接表。它看起来像一个单向的一对多关联,它将由many侧面的外键管理(您的示例中的课程)。看看这个例子和相关的解释。这是建立关联的事实上的SQL方式。one-to-many

此外,如果您需要一个连接表,您可能需要在 hibernate 文档中执行类似于此示例的操作。基本上,如果您需要单向one-to-many关联中的连接表,您可以使用属性设置为;的many-to-many元素。这有效地使关联。uniquetrueone-to-many

于 2013-08-30T17:04:07.980 回答