0

我正在尝试解决我的休眠数据库系统的问题,但我不确定我应该做错什么:

我有两个实体:顶点(Vertice)和标签。一个顶点应该有很多标签。当我在一个集合中添加一个带有标签的顶点时,顶点被保存到数据库中,标签也被保存,但顶点的 FK 列被记录为空。这意味着我无法在需要时检索标签集。感谢您的帮助!

下面是代码:

import java.util.Set;
public class Vertice {

private long id;
private Set<Tag> tags;
private String amostra = new String();
private String chave = new String();

public Vertice() {
}

public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}
public Set<Tag> getTags() {
    return tags;
}
public void setTags(Set<Tag> tags) {
    this.tags = tags;
}
public String getAmostra() {
    return amostra;
}
public void setAmostra(String amostra) {
    this.amostra = amostra;
}
public String getChave() {
    return chave;
}
public void setChave(String chave) {
    this.chave = this.geraChave(chave);
}

private String geraChave(String chave){
    String _chave = new String();
    _chave = chave;
    try{
        if(this.getTags()!=null){
            for(Tag t: this.getTags()){
                _chave = _chave + t.getTexto();
            }
        }
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return _chave;
}

}

班级标签:

public class Tag {
    private long id;
    private int linha;
    private int coluna;
    private String texto = new String();

private Vertice vertice;

public Vertice getVertice() {
    return vertice;
}
public void setVertice(Vertice vertice) {
    this.vertice = vertice;
}
public int getLinha() {
    return linha;
}
public int getColuna() {
    return coluna;
}
public void setColuna(int coluna) {
    this.coluna = coluna;
}
public void setLinha(int linha) {
    this.linha = linha;
}
public String getTexto() {
    return texto;
}
public void setTexto(String texto) {
    this.texto = texto;
}
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}

}

VerticeDAO 类的一段代码:

public boolean salvaAtualiza(Vertice VerticeArg) throws ViolacaoChaveUnicaException{

boolean ret = false;

try{
     if(VerticeArg.equals(null)){
        throw new Exception("O objeto <Vertice> foi informado vazio.");
     }
     else{
        sess.beginTransaction();
        sess.saveOrUpdate(VerticeArg);
        ret = true;
     }
    }
    catch(ConstraintViolationException e){
        throw new ViolacaoChaveUnicaException(
           "Houve uma violação de chave na tentativa " + 
              "de gravar os dados no BD");
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return ret;
}

顶点实体的 XML (Vertice.hbm.xml):

<hibernate-mapping>
<class name="xx.xxx.xxx.Vertice" table="pcommjava_vertice">
    <id name="id" column="vertice_id" type="long">
        <generator class="native" />
    </id>
    <property name="amostra">
        <column name="vertice_amostra" not-null="true" length="1920"/>
    </property>
    <property name="chave">
        <column name="vertice_chave" not-null="true" length="50" unique="true" unique-key="vertice_chave"/>
    </property>
    </class>

标记 XML 映射 (Tag.hbm.xml):

<hibernate-mapping>
<class name="xx.xxx.xxx.Tag" table="pcommjava_tag">
    <id name="id" column="tag_id" type="long">
        <generator class="native" />
    </id>
    <property name="linha">
        <column name="tag_linha" not-null="true" />
    </property>
    <property name="coluna">
        <column name="tag_coluna" not-null="true" />
    </property>
    <property name="texto">
        <column name="tag_texto" not-null="true" />
    </property>

    <many-to-one name="vertice" cascade="all" not-null="true" column="tag_vertice" class="xx.xxx.xxx.Vertice" />

</class>

最后是hibernate.cfg.xml:

 <hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">SOMEPASSWORD</property>
  <property name="hibernate.connection.url">jdbc:mysql://00.00.000.000:3306/DB2</property>
  <property name="hibernate.connection.username">developer</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.current_session_context_class">thread</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
  <!-- Automatic schema creation (begin) === -->
  <property name="hibernate.hbm2ddl.auto">create</property>
  <!-- Simple memory-only cache -->
  <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

  <mapping class="xx.xx.xxx.Vertice"
   package="xx.xx.xxx.Vertice" resource="xx/xxx/xxx/Vertice.hbm.xml"/>
  <mapping class="xx.xx.xxx.Tag"
   package="xx.xx.xxx.Tag" resource="xx/xx/xxx/Tag.hbm.xml"/>

 </session-factory>

</hibernate-configuration>
4

1 回答 1

1

问题是您没有set<Tag>vertex.hbm.xml中定义。根据我的理解,当您将 Set 放入Vertice.java时,它会变成一对多或多对多的关系。除此之外,如果将Vertice引用放在Tag.java中,它就变成了双向关系。有关更多信息,请参阅:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/collections.html

于 2012-10-04T15:30:28.537 回答