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!
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.
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();
}
}
}