How do I get the Hibernate reverse engineering tools to generate <bag> or <list> for inverse associations?

℡╲_俬逩灬. 提交于 2019-12-23 02:14:11

问题


I want to add elements to a collection that ends up getting mapped like this:

<set name="others" inverse="true" lazy="true" table="other" fetch="select">
  <key>
    <column name="otherId" not-null="true" />
  </key>
  <one-to-many class="my.pkg.OtherEntity" />
</set>

I'd like Hibernate to use instead, because I don't care about the order they're retrieved in, I just want to keep that side of the association up-to-date. According to https://www.hibernate.org/117.html, "Hibernate can add to a <bag>, <idbag> or <list> declared with inverse="true" without initializing the collection."

My problem is I don't know how to force (or suggest to) the reverse engineering tools (hibernate-tools.jar) to use <bag> or <list>--it ALWAYS uses <set>.

Thanks!


回答1:


You can customize the reverse engineering procedure with a reverse-engineering configuration file, which uses an XML syntax. I haven't tried this myself, but this example may work for you:

<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-reverse-engineering SYSTEM
    "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
    <table name="MYTABLE">
        <column name="others" type="bag"/>
    </table>
</hibernate-reverse-engineering>

If it does not work as it is, you may find more details about controlling the reverse engineering process here.




回答2:


Ever you would have List-type instead of Set-type you may use ant-java-task using:

<java classname="de.wedeaf.beans.BeanHelper" classpath="WebContent/WEB-INF/classes" args="${basedir}/JavaSource"/>

Using the class "de.wedeaf.beans.BeanHelper":

package de.wedeaf.beans;
import java.io.*;
import org.w3c.dom.NodeList;
import com.sun.org.apache.xerces.internal.parsers.DOMParser;
public class BeanHelper {
 public static void main(String[] args) throws Exception {
  DOMParser parser = new DOMParser();  
  parser.parse("file:///"+args[0]+"/hibernate.cfg.xml");
  NodeList list = parser.getDocument().getElementsByTagName("mapping");
  for (int i = 0; list.item(i) != null; i++) {
   String entityFile = args[0] + File.separatorChar + list.item(i)   .getAttributes().getNamedItem("class").getNodeValue().replace('.', File.separatorChar) + ".java";
   // Eingabe
   FileInputStream in = new FileInputStream(entityFile);
   byte[] code=new byte[in.available()];
   in.read(code);
   in.close();
   // Verarbeitung
   String content=new String(code);
   content = content.replaceAll("java.util.Set", "java.util.List");
   content = content.replaceAll("java.util.HashSet", "jav   a.util.ArrayList");
   content = content.replaceAll(" HashSet<", " ArrayList<");
   content = content.replaceAll("Set<", "List<");
       // Ausgabe
   FileWriter fw = new FileWriter(entityFile);
   fw.write(content);
   fw.close();
     }
 }
}


来源:https://stackoverflow.com/questions/2231755/how-do-i-get-the-hibernate-reverse-engineering-tools-to-generate-bag-or-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!