我在hibernate中有两个项目,一个用于XML,一个用于注释。 我的XML Mapping配置适用于复合键,而注释样式会生成错误的列。
注释样式生成此表
Hibernate: create table agent (agent_id number(10,0) not null, start_dttm timestamp not null, agent_cd varchar2(255 char), end_dttm timestamp, primary key (agent_id, start_dttm))
Hibernate: create table agent_license (agent_license_id number(10,0) not null, agent_agent_id number(10,0), agent_start_dttm timestamp, agent_id number(10,0), primary key (agent_license_id))
Hibernate: alter table agent_license add constraint FKncan0jjmt1siq0a6a19ll978m foreign key (agent_agent_id, agent_start_dttm) references agent
Hibernate: alter table agent_license add constraint FKp64d6078jnxi3hq0n0es0yhs foreign key (agent_id, agent_start_dttm) references agent
AgentLicense的冗余列,包含agent_agent_id,而不是agent_id。
这正在正确地使用XML配置 XML生成 具有列agent_id,start_dttm,agent_cd的代理表 Agent_License,列为agent_id,agent_start_dttm
代理商模型
<class name="com.poc.model.Agent" table="AGENT">
<composite-id name="id" class="com.poc.model.pk.AgentPK">
<key-property name="agentId" type="long">
<column name="AGENT_ID" precision="10" scale="0" />
</key-property>
<key-property name="startDateTime" type="timestamp">
<column name="START_DTTM" />
</key-property>
</composite-id>
<property name="agentCode" type="string">
<column name="AGENT_CD" length="20" />
</property>
<set name="agentLicenses" table="AGENT_LICENSE" inverse="true" lazy="true" fetch="select">
<key>
<column name="AGENT_ID" precision="10" scale="0" />
<column name="AGENT_START_DTTM" />
</key>
<one-to-many class="com.poc.model.AgentLicense" />
</set>
</class>
AgentLicense模型
<class name="com.poc.model.AgentLicense" table="AGENT_LICENSE">
<id name="agentLicenseId" type="long">
<column name="AGENT_LICENSE_ID" precision="10" scale="0" />
<generator class="assigned" />
</id>
<many-to-one name="agent" class="com.poc.model.Agent" fetch="select">
<column name="AGENT_ID" precision="10" scale="0" />
<column name="AGENT_START_DTTM" />
</many-to-one>
<property name="licenseNum" type="string">
<column name="LICENSE_NUM" length="40" />
</property>
</class>
代理PK模型
<class name="com.poc.model.Agent" table="AGENT">
<composite-id name="id" class="com.poc.model.pk.AgentPK">
<key-property name="agentId" type="long">
<column name="AGENT_ID" precision="10" scale="0" />
</key-property>
<key-property name="startDateTime" type="timestamp">
<column name="START_DTTM" />
</key-property>
</composite-id>
<property name="agentCode" type="string">
<column name="AGENT_CD" length="20" />
</property>
<set name="agentLicenses" table="AGENT_LICENSE" inverse="true" lazy="true" fetch="select">
<key>
<column name="AGENT_ID" precision="10" scale="0" />
<column name="AGENT_START_DTTM" />
</key>
<one-to-many class="com.poc.model.AgentLicense" />
</set>
</class>
正确注释是这个
Result table generation
Agent Table with column agent_id,start_dttm,agent_cd
Agent_License with column agent_id,agent_start_dttm
public class Agent {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "agentId", column = @Column(name = "AGENT_ID", nullable = false)),
@AttributeOverride(name = "startDateTime", column = @Column(name = "START_DTTM", nullable = false)) })
private AgentPK agentPK;
@Column(name="AGENT_CODE")
private String agentCode;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "agent")
private Set<AgentLicense> agentLicenses = new HashSet<AgentLicense>();
}
public class AgentLicense {
@Id
@Column(name = "AGENT_LICENSE_ID", unique = true, nullable = false)
private long agentLicenseId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "AGENT_ID", referencedColumnName = "AGENT_ID"),
@JoinColumn(name = "AGENT_START_DTTM", referencedColumnName = "START_DTTM") })
private Agent agent;
}
@Embeddable
public class AgentPK implements Serializable{
@Column(name = "AGENT_ID", nullable = false, precision = 10, scale = 0)
private long agentId;
@Column(name = "START_DTTM", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date startDateTime;
//HashCode and equals here
}
正确答案
答案 0 :(得分:0)
修正AgentLicense
Agent
的逆映射
public class Agent {
// ...
@OneToMany(mappedBy = "agent")
@JoinColumns( ... )
private Set<AgentLicense> agentLicenses;
}