当我运行以下课程时:
@Entity
public class SurveyResponse {
@Id
private String assignmentId;
public String getAssignmentId() {
return assignmentId;
}
public void setAssignmentId(String assignmentId) {
this.assignmentId = assignmentId;
}
@ElementCollection
@MapKeyColumn(name="topic")
@Column(name="relevance")
@CollectionTable(name="RelevanceScore", joinColumns=@JoinColumn(name="assignmentId"))
Map<Topic, Relevance> topicRelevance = new HashMap<Topic, Relevance>(); // maps from attribute name to value
public static void main( String[] args ) {
Configuration config = new Configuration();
config.addAnnotatedClass(SurveyResponse.class);
config.configure( "hibernate.cfg.xml" );
new SchemaExport(config).create( true, true );
}
}
它按预期创建表 SurveyResponse 和 RelevanceScore。但是,当我将 @Id 注释放在主键的 getter 上方时......
@Entity
public class SurveyResponse {
private String assignmentId;
@Id
public String getAssignmentId() {
return assignmentId;
}
...
}
...它无法生成表 RelevanceScore。为什么?