[英]Hibernate many-to-one relationship through java.util Set
我正在尝试解决我的休眠数据库系统的问题,但是我不确定该怎么做:
我有两个实体:顶点(Vertice)和标签。 一个顶点应具有多个标签。 当我在其中的Set中添加带有标签的顶点时,这些顶点也会保存到DB中,这些标签也会保存,但顶点的FK列将记录为null。 这意味着我无法在需要时检索标签集。 感谢您的帮助!
下面是代码:
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>
问题是您尚未在vertex.hbm.xml中定义set<Tag>
。 根据我的理解,将Set放入Vertice.java时 ,它成为一对多或多对多关系。 除此之外 ,如果将Vertice引用放在Tag.java中,它将成为双向关系。 有关更多信息,请参见:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/collections.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.