gpt4 book ai didi

java - Hibernate通过java.util设置多对一关系

转载 作者:行者123 更新时间:2023-12-01 15:07:56 25 4
gpt4 key购买 nike

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

我有两个实体:顶点(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>

最佳答案

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

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

关于java - Hibernate通过java.util设置多对一关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12729891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com