我正在尝试修复我的hibernate数据库系统的问题,但我不确定我应该做错什么:
我有两个实体:顶点(Vertice)和Tag。一个顶点应该有很多标签。当我在Set里面添加带有Tags的顶点时,顶点会保存到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;
}
Vertex Entity的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>
答案 0 :(得分:1)
问题是您尚未在 vertex.hbm.xml 中定义set<Tag>
。根据我的理解,当你把Set放在 Vertice.java 中时,它会变成一对多或多对多的关系。 除此之外,如果您在 Tag.java 中添加 Vertice 引用,它就会变成双向关系。
有关详细信息,请参阅:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/collections.html