【问题标题】:jpa hibernate one-to-many bidirectional [duplicate]jpa休眠一对多双向[重复]
【发布时间】:2016-10-31 00:58:41
【问题描述】:

我的一对多关系有问题。 我的问题是当我将租户添加到公寓时,它不会将租户中的列值设置为 ApartmentID。问题是我与其他班级的关系完全相同,而且他们工作得很好......有谁知道为什么它不工作? 谢谢

公寓:

@Entity
public class Apartment {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="ApartmentID",unique = true, nullable = false)
    public int apartmentID;

    @OneToMany(mappedBy = "apartment", cascade = CascadeType.ALL)
    private List<Tenant> tenants;
}

租户:

@Entity
public class Tenant {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="TenantID",unique = true, nullable = false)
    public int tenantID;

    @ManyToOne
    @JoinColumn(name = "ApartmentID")
    private Apartment apartment;
}

【问题讨论】:

    标签: java hibernate jpa one-to-many bidirectional


    【解决方案1】:

    在您的双向关系中,Tenant 是关系的所有者。由于您坚持使用Apartment,因此您必须手动设置 tenantapartment。如果您想摆脱手动设置,请按如下方式更改您的@OneToMany

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name="ApartmentID",referencedColumnName="ApartmentID")
    private List<Tenant> tenants;
    

    顺便说一下,如果您将 tenantapartment 保持一致,请使用您当前的示例; 公寓将自动持久化。

    【讨论】:

      【解决方案2】:

      您不会在此处显示所有代码。所以我不确定你的问题是否准确。但是,我只是关注Hibernate Many to One Bidirectional Mapping Annotation Example 的帖子并与您的实体一起构建项目。

      这是我将租户添加到公寓中的主要课程。

      public static void main(String[] args) {
      
              Tenant tenant = new Tenant();
              Apartment apartment = new Apartment();
              tenant.setApartment(apartment);
              List<Tenant> tenants = new ArrayList<Tenant>();
              tenants.add(tenant);
              apartment.setTenants(tenants);
      
              Session session = HibernateUtil.getSessionFactory().openSession();
              session.beginTransaction();
              session.persist(apartment);
              session.getTransaction().commit();
      
              session.close();
          }
      

      休眠配置

      <hibernate-configuration>
          <session-factory>
              <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
              <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
              <property name="hibernate.connection.username">javabycode</property>
              <property name="hibernate.connection.password">mypassword</property>
              <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/javabycode</property>
              <property name="show_sql">false</property>
              <property name="hbm2ddl.auto">update</property>                
              <property name="format_sql">false</property>
              <mapping class="com.javabycode.hibernate.model.Tenant"/>
              <mapping class="com.javabycode.hibernate.model.Apartment"/>        
          </session-factory>
      </hibernate-configuration>
      

      休眠实用程序:

      public class HibernateUtil {
          private static final SessionFactory sessionFactory;
      
          static {
              try {
                  Configuration configuration = new Configuration();
                  configuration.configure();
                  ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                          .applySettings(configuration.getProperties()).build();
                  sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry);
              } catch (Throwable ex) {
                  System.err.println("Session Factory could not be created." + ex);
                  throw new ExceptionInInitializerError(ex);
              }
          }
      
          public static SessionFactory getSessionFactory() {
              return sessionFactory;
          }
      
      }
      

      一切正常! ApartmentID 已在 Tanent 表中分配了一个值。

      【讨论】:

      • 问题是你正在为每一边设置值,我不想这样做。如果我做 apartment.setTenants() 并坚持/合并它,我应该让租户更新其领域中的公寓,但我没有。奇怪的是,我也有 Housing and Landlord,它有一对多的关系,而且工作得很好。如果你愿意,我可以添加我的模型的粘贴箱,但我相信一切都很好
      【解决方案3】:
      **please refer the example you will get an idea**
      
      --------------------------------------------------------
      
      @Entity
      public class Person {
          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          private int id;
          private String name;
          @OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
          private List<Address> addresses;
      }
      
      @Entity
      public class Address {
          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          private int id;
          private String street;
          private int houseNumber;
          private String city;
          private int zipCode;
          @ManyToOne(fetch = FetchType.LAZY)
          private Person person;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-03-23
        • 2021-12-23
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-02
        • 1970-01-01
        相关资源
        最近更新 更多