我有一个映射的POJO,我需要坚持。在那个POJO中我有另一个POJO,用外键绑定 现在,当我创建一个新对象并使用Hibernate持久化时,Hibernate是否仍然存在嵌入式POJO,或者我必须手动执行它? 基本上问题是关于“反向级联”。
在我的情况下,这不会发生,如果我事先没有保留嵌入式POJO,我会收到异常。
编辑:这里有一些代码,我省略了一些细节(setter& getters): 这是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;
}
}
这是地址类:
@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;
}
}
这是我在Spring中使用的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>
答案 0 :(得分:1)
不,除非你告诉Hibernate使用
,否则不会级联@ManyToOne(cascade = CascadeType.PERSIST)
请参阅http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#objectstate-transitive
答案 1 :(得分:0)
@ManyToOne(cascade = CascadeType.DETACH)
@JoinColumn(name = "address_id", nullable = false, unique = true)
public Address getAddress() {
return this.address;
}
也许上面的代码解决了你的问题
您可以使用上面的一些代码来说明hibernate如何对连接的POJO起作用。
我希望它有所帮助