CREATE TABLE `incident` (
`author_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
PRIMARY KEY (`author_id`)
);
CREATE TABLE `adr` (
`author_id` int(11) NOT NULL,
`title` varchar(128) NOT NULL,
`description` varchar(512) NOT NULL,
`published` date NOT NULL,
PRIMARY KEY (`author_id`),
KEY `author_fk` (`author_id`),
CONSTRAINT `author_fk` FOREIGN KEY (`author_id`) REFERENCES `incident` (`author_id`)
);
这是事件和adr之间的一对一映射。 在adr表中,author_id是PK& FK。
我们如何为这对一对一关系之王创建一个实体类。
尝试过的解决方案
//事件表实体类
Entity
@Table(name="INCIDENT")
public class Incident implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="IR_TB_INCIDENT_HDR_INCIDENTID_GENERATOR", sequenceName="IR_SEQ_INCIDENT_ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="IR_TB_INCIDENT_HDR_INCIDENTID_GENERATOR")
@Column(name="AUTHOR_ID")
private int authorId;
@Column(name="NAME")
private String name;
@Column(name="EMAIL")
private String email;
@OneToOne(cascade = {CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE}, fetch = FetchType.LAZY, mappedBy="Incident")
private Adr adr;
/** constructors , getters & sertters **/
}
compile time Exception in Incident.java
In attribute 'adr', the "mapped by" attribute 'incident' has an invalid mapping type for this relationship.
如何定义adr实体,以便作者ID引用事件表中的作者ID 试过这个但没有工作
@Entity
@Table(name="ADR")
public class Adr implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="INCIDENT_ID")
private Incident Incident;
@Column(name="TITLE")
private String title;
/** rest of the fields and methos **/
}