我有两个实体PointOfInterest(从这里开始称为POI)及其地址。我想定义一对一的双向映射,在两者之间使用共享主键,POI作为所有者实体。我使用的是postgreSQL DB,POI表有POIId作为PK,由DB中定义的序列生成器生成。地址表具有列POIId,它是地址表的PK,也是FK到POI表的POIId列,以及它自己的其他列。
**PointOfInterest.java**
@Entity
@Table(name = "\"POI\"")
public class PointOfInterest implements Serializable {
private static final long serialVersionUID = -5406785879200149642L;
@Id
@Column(name="\"POIId\"", nullable=false)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="poi_seq_poiid_generator")
@SequenceGenerator(name = "poi_seq_poiid_generator", sequenceName = "poi_seq_poiid", allocationSize=1)
private Long poiId;
@OneToOne(mappedBy = "poi",cascade = CascadeType.PERSIST)
private Address address;
//Other fields
-----------------------------------------------------------------
**Address.java**
@Entity
@Table(name="\"Address\"")
public class Address implements Serializable{
private static final long serialVersionUID = -7146133528411371107L;
@Id
@GeneratedValue(generator="sharedPrimaryKeyGenerator")
@GenericGenerator(name="sharedPrimaryKeyGenerator",strategy="foreign",parameters = @Parameter(name="property", value="poi"))
@Column(name="\"POIId\"")
private Long poiId;
@OneToOne
@PrimaryKeyJoinColumn
private PointOfInterest poi;
使用session.saveOrUpdate(poi)保存POI对象之前。我实例化并设置POI对象的所有其他属性。然后,从我的逻辑中的单独方法获取Address对象,并执行类似的操作。
PointOfInterest poi = methodCallToGetPOI();
Address address = methodCallToGetAddress();
poi.setAddress(address);
address.setPOI(poi);
//Send POI object to DB layer to an appropriate method which does:
session.saveOrUpdate(poi);
session.flush();
当我看到生成的查询时,我看到类似这样的内容:
Hibernate:
select
nextval ('poi_seq_poiid')
Hibernate:
insert
into
"POI"
("CreatedBy", "CreatedTime", "LocationGeographic", "ModifiedBy", "ModifiedTime", "Name", "POICode", "Radius", "POIType", "POIId")
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
所以,显然hibernate没有在地址表中创建一个insert语句。我在互联网上到处搜索并比较了我的映射,它们似乎是正确的。但是,地址表中没有插入任何行。当我调试流时,我发现地址对象正在实例化并填充。请帮我弄清楚这是为什么。我能想到的唯一原因是我使用的是序列生成器,并且在互联网上的所有示例和代码片段中,每个人都使用了hibernate的自动生成策略,所以,是不是因为这个?我不能使用自动生成密钥,我只能使用我的数据库序列。如果这些注释不起作用,请提出一些替代方案。
我使用Spring和Hibernate与Spring框架版本4.0.5.RELEASE和从org.springframework.orm.hibernate4.LocalSessionFactoryBean获得的会话工厂和从org.springframework.orm.hibernate4.HibernateTransactionManager获得的事务管理器。
答案 0 :(得分:1)
您也不想在地址上生成PK(即使使用ad-hoc生成器)。
由于这两个实体共享PK,因此生成可能只发生一次。然后,映射必须负责使用相同的值填充POI_ID
和ADDRESS_ID
。
奇怪的是,@PrimaryKeyJoinColumn
在Hibernate 5.2.2上不再起作用,但这里是一个等效的映射:
@Entity
@Table(name = "POI")
public class PointOfInterest implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "poi_seq_poiid_generator")
@SequenceGenerator(name = "poi_seq_poiid_generator", sequenceName = "poi_seq_poiid", allocationSize = 1)
@Column(name = "POI_ID")
private Long id;
@OneToOne(mappedBy = "poi", cascade = CascadeType.ALL, orphanRemoval = true)
private Address address;
@Column
private String name;
...
}
@Entity
@Table(name = "Address")
public class Address implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@OneToOne
@JoinColumn(name = "ADDRESS_ID")
private PointOfInterest poi;
@Column
private String name;
...
}
使用Hibernate DDL生成,这是生成的SQL:
create table Address (name varchar(255), ADDRESS_ID bigint not null, primary key (ADDRESS_ID)) ENGINE=InnoDB
create table POI (POI_ID bigint not null, name varchar(255), primary key (POI_ID)) ENGINE=InnoDB
alter table Address add constraint FK_Address_ADDRESS_ID foreign key (ADDRESS_ID) references POI (POI_ID)
答案 1 :(得分:0)
经过几个小时的代码试验,发现这种一对一的双向映射似乎只有在指定cascade = cascadeType.ALL时才有效。我不希望这样,因为在保存POI时,地址字段由内部API调用填充,这些调用基于POI的地理位置。因此,当有人试图更新POI时,他们永远不会知道它的地址,因此它将为空。一旦更新发生,这将为该特定POI将Address表行设置为null。但是,现在就要为它做好准备。但是,仍然不确定为什么必须为这样的映射提供cascadeType.ALL。这肯定是合乎逻辑的,但没有解释为什么cascadeType.PERSIST不能工作。
此外,我使用的映射(如@GenericGenerator)是特定于hibernate的,因此这段代码以hibernate特定的方式而不是JPA进行映射。另外,@ GeGeneGenerator只是告诉hibernate它应该填充地址的Id的方法,其中Id是由POI按序列生成的。