我有一個映射POJO,我需要堅持。在那個POJO中,我有另一個POJO,綁定了外鍵。
現在,當我創建一個新對象並使用Hibernate進行存儲時,Hibernate是否也會保留嵌入的POJO,或者我必須手動執行它?
基本上問題是關於「反向級聯」。基本的休眠持久性
在我的情況下,這種情況不會發生,如果我沒有事先保存嵌入的POJO,我會得到一個異常。
編輯:下面是一些代碼,我省略了一些細節(設置器&獲取器): 這是Person類,如您所見,它有一個Address類嵌入其中。
@Entity
public class Person {
protected Address address;
protected String privateName;
...
/* More members */
@ManyToOne
@JoinColumn(name = "address_id", nullable = false, unique = true)
public Address getAddress() {
return this.address;
}
public void setAddress(Address address) {
this.address = address;
}
@Column(name = "private_name", nullable = false, length = 45)
public String getPrivateName() {
return this.privateName;
}
public void setPrivateName(String privateName) {
this.privateName = privateName;
}
}
這是Address類:
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
@SuppressWarnings("serial")
@Entity
@Table(name = "address")
public class Address implements java.io.Serializable {
private Integer id;
private Country country;
private State state;
private String city;
private String street;
private String number;
private String postcode;
private float longitude;
private float latitude;
public Address() {
}
public Address(Integer id, Country countries, String city, String street,
String number, String postcode, float longitude, float latitude) {
this.id = id;
this.country = countries;
this.city = city;
this.street = street;
this.number = number;
this.postcode = postcode;
this.longitude = longitude;
this.latitude = latitude;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name = "state")
public State getState() {
return this.state;
}
public void setState(State state) {
this.state = state;
}
@ManyToOne
@JoinColumn(name = "country", nullable = false)
public Country getCountry() {
return this.country;
}
}
這裏是Hibernate配置我使用同春:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:my_db;INIT=CREATE SCHEMA IF NOT EXISTS my_db;DB_CLOSE_DELAY=-1" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="packagesToScan" value="com.tra.la.bla" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
你可以分享你的hbm.xml文件? – melihcelik
只要你已經正確映射它們,Hibernate就會堅持這兩個對象。我們需要查看類和任何Hibernate配置來解釋爲什麼它不會發生。 –
你的意思是「嵌入」的方式,實體A由實體B組成,或者你的意思是嵌入式,所以實體A由嵌入式B組成? –