2013-08-23 193 views
0

LevelTerm.hbm.xml文件是:一個一對多的Hibernate映射

<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」。但沒有創建表。

回答

0

不,您的映射不應該生成連接表。它看起來像是一個單向的一對多關聯,它將通過many一側的外鍵進行管理(您的示例中的課程)。看看this example和相關的解釋。這是具有one-to-many關聯的事實上的SQL方式。另外,如果你需要一個連接表,你可能需要在hibernate文檔中做類似this example的操作。基本上,如果您需要在單向關聯中使用連接表,則使用many-to-many元素,其中unique屬性設置爲true;這有效地使協會one-to-many