我有 2 张桌子:
CREATE TABLE `addr1` (
`id` int(11) DEFAULT NULL,
`addr2` varchar(20) DEFAULT NULL
) ENGINE=InnoDB;
CREATE TABLE `address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`address` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
的主键addr1
是从地址表中设置的。我使用联合继承创建了我的实体
@Entity
@Table(name="addr1")
public class Addr1 extends Address {
@Column(name="addr2")
private String addr2;
// getters and setters follows
}
@Entity
@Table(name = "address")
@Inheritance(strategy=InheritanceType.JOINED)
public class Address implements Serializable {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "address", nullable = false, length = 250, insertable = true)
private String address;
// getters and setters follows
}
现在我在地址表中有一行 ID 为 2。我想在 addr1 表中插入一行 ID 相同 (2)。我试过
Address addr = em.getReference(Address.class, new Integer(4));
Addr1 addr1 = new Addr1(addr);
addr1.setAddr2("dsdsd");
em.persist(addr1);
但我收到一个错误
javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:分离的实体传递给坚持:Addr1
请帮我。
问候, 普拉文