jpa hibernate 一对多双向

jpa hibernate one-to-many bidirectional

我的一个一对多关系有问题。 我的问题是当我将租户添加到公寓时,它没有将租户中的列值设置为 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;
}

您没有在此处显示所有代码。所以我不确定你的问题到底是什么。但是,我只是按照 post Hibernate Many to One Bidirectional Mapping Annotation Example 并使用您的实体构建项目。

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

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

中分配了一个值

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

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

顺便说一下,如果您坚持 tenant with apartment,则使用您当前的示例; 公寓将自动保留。

**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;
}