如何使用 Hibernate 逆向工程工具生成或<列表>对于逆关联?

发布于 2024-08-21 15:43:03 字数 651 浏览 5 评论 0原文

我想将元素添加到最终像这样映射的集合中:

<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>

我希望使用 Hibernate,因为我不关心它们检索的顺序,我只想保留关联的那一侧最新。根据 https://www.hibernate.org/117.html,“Hibernate 可以添加到使用 inverse="true" 声明的而不初始化集合。

我的问题是我不知道如何强制(或建议)逆向工程工具(hibernate-tools.jar)使用——它总是使用

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所的.畏惧 2024-08-28 15:43:03

您可以使用逆向工程配置文件(使用 XML 语法)自定义逆向工程过程。我自己还没有尝试过,但这个示例可能对您有用:

<?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>

如果它不起作用,您可能会找到有关控制逆向工程过程的更多详细信息 此处

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.

各自安好 2024-08-28 15:43:03

如果您拥有 List 类型而不是 Set 类型,您可以使用 ant-java-task 使用:

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

使用类“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();
     }
 }
}

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();
     }
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文