【发布时间】:2015-04-22 05:16:50
【问题描述】:
我开始在项目中使用注释作为理解它们的借口,我有一个问题。
我为我的数据库中的每个表创建了域类,并使用注释将成员与列连接起来,使用验证和类似的东西。我还通过使用 uniqueConstraints 来使用唯一索引约束,并且在我有多对一或多对多关系的情况下,我使用可连接的连接列。
我的问题是两个,可能有点幼稚,
1)我在持久化xml中设置了hibernate.hbm2ddl.auto创建,然后运行app,通过app创建数据库。然后我去了数据库并导出了模式创建 sql 文件。这是获取创建模式 sql 的有效做法吗?此外,我注意到虽然我在外键和索引的注释中使用的常量名称被转移,但在我的多对多关系中,名称是生成的名称,而不是我指定的名称。除了我可能做错了什么之外,还有其他原因吗?
2) 如果我从上面获取架构,请将约束的名称修复为我想要的,然后运行它以创建数据库,并运行应用程序,但将持久化 xml 选项 hibernate.hbm2ddl.auto 设置为由于约束的名称可能与注释中的名称不匹配,因此更新会导致问题吗?
对于多对多关系,这是两个域类
@Entity
@Table(name = "SOLUTION")
public class Solution {
@Id
@Column(name = "SOLUTION_CODE", length = 20, nullable = false)
@Size(min = 1, max = 20, message = "The solution code size is not within limits (0-20)")
@NotNull(message = "The solution code cannot be null")
private String code;
@Column(name = "SOLUTION_DESCRIPTION", length = 200, nullable = false)
@Size(min = 1, max = 200, message = "The solution description is not within limits (0-200)")
@NotNull(message = "The solution description cannot be null")
private String description;
@ManyToMany(mappedBy = "solutionList")
private List<Category> categories;
public Solution() {
}
}
@Entity
@Table(name = "CATEGORY")
public class Category {
@Id
@Column(name = "CATEGORY_CODE", length = 20, nullable = false)
@Size(min = 1, max = 20, message = "The category code size is not within limits (0-20)")
@NotNull(message = "The category code cannot be null")
private String code;
@Column(name = "CATEGORY_DESCRIPTION", length = 200, nullable = false)
@Size(min = 1, max = 200, message = "The category description is not within limits (0-200)")
@NotNull(message = "The category description cannot be null")
private String description;
@ManyToMany
@JoinTable(
name = "SOLUTION_CATEGORY_MAPPING",
joinColumns = @JoinColumn(
name = "CATEGORY_CODE",
referencedColumnName = "CATEGORY_CODE",
foreignKey = @ForeignKey(name = "FK_SOLUTION_CATEGORY_CATEGORY_CODE")),
inverseJoinColumns = @JoinColumn(
name = "SOLUTION_CODE",
referencedColumnName = "SOLUTION_CODE",
foreignKey = @ForeignKey(name = "FK_SOLUTION_CATEGORY_SOLUTION_CODE")))
private List<Solution> solutionList;
public Category() {
}
}
我希望外键约束具有我指定的名称,但它们只是生成了名称
编辑:另外我想知道是否有人可以过度使用注释以及它们是否会影响性能?
【问题讨论】: