我的一对多关系存在问题。 我的问题是当我将租户添加到公寓时,它不会将租户中的列值设置为ApartmentID。问题是我与其他班级有完全相同的关系,他们工作得很好......有没有人知道为什么它不是在wotking? 感谢
公寓:
@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;
}
答案 0 :(得分:0)
您不会在此处显示所有代码。因此,我不确定是否完全解决您的问题。但是,我只关注帖子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配置
<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>
Hibernate Util:
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表中分配了一个值。
答案 1 :(得分:0)
在双向关系中,Tenant
是关系的所有者。由于您持续Apartment
,因此您必须手动设置租户的公寓。如果您想摆脱手动设置,请更改@OneToMany
,如下所示:
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="ApartmentID",referencedColumnName="ApartmentID")
private List<Tenant> tenants;
顺便说一句,如果您使用公寓来坚持租户,请使用您当前的示例; 公寓将自动保留。
答案 2 :(得分:0)
**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;
}