2012-05-18 251 views
0

我有點迷路了Hibernate集合映射。嘗試添加拋出以下在Eclipse Hibernate的角度來看,持久性配置:Hibernate集合映射

Invalid ORM mapping file. 
Error parsing XML (line14 : column 16): The content of element type "list" must match "(meta*,subselect?,cache?,synchronize*,comment?,key,(index|list-index),(element|one-to-many|many-to-many|composite-element|many-to-any),loader?,sql-insert?,sql-update?,sql-delete?,sql-delete-all?,filter*)". 
Error parsing XML (line17 : column 15): The content of element type "map" must match "(meta*,subselect?,cache?,synchronize*,comment?,key,(map-key|composite-map-key|map-key-many-to-many|index|composite-index|index-many-to-many|index-many-to-any),(element|one-to-many|many-to-many|composite-element|many-to-any),loader?,sql-insert?,sql-update?,sql-delete?,sql-delete-all?,filter*)". 

實際映射配置如下:

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
    <class name="core.Server" table="server" schema="imdb"> 
     <id name="serverId" type="java.lang.Integer"> 
      <column name="SERVER_ID" /> 
      <generator class="identity" /> 
     </id> 
     <list name="sessions" inverse="true"> 
      <key column="serverID" not-null="true"/> 
      <list-index column="SERVER_SESSION_NUMBER" />   
      <one-to-many class="core.Session"/> 
     </list> 
     <map name="users" inverse="true"> 
      <key column="serverID" not-null="true"/> 
      <map-key type="string" column="SERVER_USER_NUMBER"/> 
      <one-to-many class="core.User"/> 
     </map> 
    </class> 
</hibernate-mapping> 

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
    <class name="core.User" table="user" schema="imdb"> 
     <id name="userId" type="java.lang.Integer"> 
      <column name="USER_ID" /> 
      <generator class="identity" /> 
     </id> 
     <property name="login" column="LOGIN" type="string" />   
     <property name="online" column="ONLINE" type="boolean" /> 
     <property name="last_message" column="LAST_MESSAGE" type="string" /> 
     <property name="newmessage" column="NEW_MESSAGE" type="boolean" /> 
     <many-to-one name="serverID" column="SERVER_ID" class="core.Server"/> 
     <list name="sessions" inverse="true"> 
      <key column="USER_ID" not-null="true"/> 
      <one-to-many class="core.Session"/> 
      <list-index column="USER_SESSION_NUMBER" /> 
     </list>  
    </class> 
</hibernate-mapping> 

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
    <class name="core.Session" table="session" schema="imdb"> 
     <id name="sessionID" type="java.lang.Integer"> 
      <column name="SESSION_ID" /> 
      <generator class="identity" /> 
     </id> 
     <many-to-one name="serverID" column="SERVER_ID" class="core.Server"/> 
     <many-to-one name="userID" column="USER_ID" class="core.User/> 
    </class> 
</hibernate-mapping> 

感謝您的幫助!

回答

2

訂單是特定的(並在錯誤消息中給予您)。這不是休眠的事情,它是一個XML模式的東西。

因此,獲得正確的標籤的順序,如: <列表索引>需要是前<一個一對多> 等

編輯: 並非所有的表都是錯誤的。第一個是好的。第二個有兩個錯誤,應該是

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
    <class name="core.User" table="user" schema="imdb"> 
     <id name="userId" type="java.lang.Integer"> 
      <column name="USER_ID" /> 
      <generator class="identity" /> 
     </id> 
     <property name="login" column="LOGIN" type="string" />   
     <property name="online" column="ONLINE" type="boolean" /> 
     <property name="last_message" column="LAST_MESSAGE" type="string" /> 
     <property name="newmessage" column="NEW_MESSAGE" type="boolean" /> 
     <list name="sessions" inverse="true"> 
      <key column="USER_ID" not-null="true"/> 
      <list-index column="USER_SESSION_NUMBER" /> 
      <one-to-many class="core.Session"/>   
     </list> 
     <many-to-one name="serverID" column="SERVER_ID" class="core.Server"/> 
    </class> 
</hibernate-mapping> 

你的第三個是錯誤的,因爲它下課=「core.User缺少一個報價 - 並且應該

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
    <class name="core.Session" table="session" schema="imdb"> 
     <id name="sessionID" type="java.lang.Integer"> 
      <column name="SESSION_ID" /> 
      <generator class="identity" /> 
     </id> 
     <many-to-one name="serverID" column="SERVER_ID" class="core.Server"/> 
     <many-to-one name="userID" column="USER_ID" class="core.User"/> 
    </class> 
</hibernate-mapping> 
+0

事實是,列表 - index * is * before one-to-many。 –

+0

感謝您的快速回答。對我而言,訂單可能很重要,但我會嘗試這種做法並不意味着這一點 – Galli

+0

JB Nizet - 不在第二個區塊中,它不是 – Woody