Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一对多双向映射_Java_Hibernate - Fatal编程技术网

Java 一对多双向映射

Java 一对多双向映射,java,hibernate,Java,Hibernate,我在hibernate项目中有以下文件: Customer.java、Request.java、Client.java和hibernate.cfg.xml如下: Customer.java @Entity @Table(name="customers") public class Customer { @Id @Column(name="cid") @GeneratedVal

我在hibernate项目中有以下文件:

Customer.java、Request.java、Client.java和hibernate.cfg.xml如下:

Customer.java

        @Entity
        @Table(name="customers")
        public class Customer {

            @Id
            @Column(name="cid")
            @GeneratedValue(strategy=GenerationType.AUTO)
        private int cid;
            @Column(name="name")
        private String name;
            @Column(name="phone")
        private long phone;

            @OneToMany(mappedBy="customer")
        private Set<Request> requests;


        public Customer() {
            super();
        }
        public Customer(String name, long phone) {
            super();
            this.name = name;
            this.phone = phone;
        }
        public int getCid() {
            return cid;
        }
        public void setCid(int cid) {
            this.cid = cid;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public long getPhone() {
            return phone;
        }
        public void setPhone(long phone) {
            this.phone = phone;
        }
        public Set<Request> getRequests() {
            return requests;
        }
        public void setRequests(Set<Request> requests) {
            this.requests = requests;
        }
        @Override
        public String toString() {
            return "Customer [cid=" + cid + ", name=" + name + ", phone=" + phone
                    +"]";
        }
        }
hibernate.cfg.xml

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ahamdb</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">root</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">create</property>     

            <mapping class="com.kar.hibernate.Customer" />
    <mapping class="com.kar.hibernate.Request" />
        </session-factory>
    </hibernate-configuration>
它没有更新请求表中的外键列,我不明白为什么它会这样 有人能帮忙吗


我想知道我做的是否正确,如果没有,有人能给出正确的解决方案吗?

这个问题与反向映射有关。如果我们声明:
@OneToMany(mappedBy=“customer”)
,即
mappedBy
,我们指示Hibernate:

关系的另一端会关心持久性

而且,如果另一端(请求)知道它,它就会正确地分配客户。因此,这应该解决以下问题:

...
cust.setRequests(requests);
req1.setCustomer(cust);
req2.setCustomer(cust);
Hibernate现在有足够的信息来正确插入/更新关系

这一阅读也应有助于:

  • mkyong

您好,感谢您的回复,它正在与:req1.setCustomer(cust)合作;要求2.设置客户(客户);但是我想知道我应该在current实现中做些什么来让它工作:cust.setRequests(requests);无论如何,谢谢你的帮助,我强烈建议:使用我在回答中向你展示的方法。这将生成最高效的SQL语句。任何其他映射都会导致更复杂的处理1)集合项的一次特殊插入2)与父项相关的下一次更新。此外,集合的更新将更加复杂。事实上,您的映射是最好的。只需调整代码中的几行即可设置关系的双方。这就是。。。真正地享受冬眠;)只需调整代码中的几行即可设置关系的双方。你能告诉我这段代码吗:很抱歉搞混了,但这几行是我答案的内容。我的意思是,总是分配两端。将子项放入父项集合,并将父项引用分配给每个子项。就这样。这些代码行将为Hibernate提供足够的信息—所有这些都将正确地持久化。好运气,如果这有帮助的话;)享受冬眠
        public class Client{
        public static void main(String[] args) {
            Transaction tx=null;
            try
            {
            SessionFactory sessionFactory=AHibernateUtil.getSessionFactory();
            Session session=sessionFactory.openSession();
            tx=session.beginTransaction();

            Customer cust=new Customer("bnuj",1111);
            session.save(cust);

        Request req1=new Request("4-1-14", "desc1", "active");
            session.save(req1);

            Request req2=new Request("4-2-14", "desc2", "unactive");
            session.save(req2);

            Set<Request> requests=new HashSet<Request>();
            requests.add(req1);
            requests.add(req2);
            cust.setRequests(requests);

        tx.commit();
        session.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
                if(tx!=null)
                tx.rollback();
            }
        }
        }
    Hibernate: insert into customers (name, phone) values (?, ?)
    Hibernate: insert into requests (cid, description, reqdate, status) values (?, ?, ?, ?)
    Hibernate: insert into requests (cid, description, reqdate, status) values (?, ?, ?, ?)
...
cust.setRequests(requests);
req1.setCustomer(cust);
req2.setCustomer(cust);