gpt4 book ai didi

java - 在 Hibernate 教程中添加约束

转载 作者:行者123 更新时间:2023-11-30 10:48:27 25 4
gpt4 key购买 nike

这个例子来自JBOSS的hibernate教程。链接在那里: http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#embeddables .

示例 79. 单向@OneToMany 关联

@Entity(name = "Person")
public static class Person {

@Id
@GeneratedValue
private Long id;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Phone> phones = new ArrayList<>();

public Person() {
}

public List<Phone> getPhones() {
return phones;
}
}

@Entity(name = "Phone")
public static class Phone {

@Id
@GeneratedValue
private Long id;

private String number;

public Phone() {
}

public Phone(String number) {
this.number = number;
}

public Long getId() {
return id;
}

public String getNumber() {
return number;
}
}
CREATE TABLE Person (
id BIGINT NOT NULL ,
PRIMARY KEY ( id )
)

CREATE TABLE Person_Phone (
Person_id BIGINT NOT NULL ,
phones_id BIGINT NOT NULL
)

CREATE TABLE Phone (
id BIGINT NOT NULL ,
number VARCHAR(255) ,
PRIMARY KEY ( id )
)

ALTER TABLE Person_Phone
ADD CONSTRAINT UK_9uhc5itwc9h5gcng944pcaslf
UNIQUE (phones_id)

ALTER TABLE Person_Phone
ADD CONSTRAINT FKr38us2n8g5p9rj0b494sd3391
FOREIGN KEY (phones_id) REFERENCES Phone

ALTER TABLE Person_Phone
ADD CONSTRAINT FK2ex4e4p7w1cj310kg2woisjl2
FOREIGN KEY (Person_id) REFERENCES Person

我的问题是,在这个例子中:

1) 为什么 ADD CONSTRAINT 看起来像机器生成的字符串“FKr38us2n8g5p9rj0b494sd3391”?不应该使用更有意义的约束手动创建它吗?

2) 在这种情况下 ADD CONSTRAINT 是可选的吗?

最佳答案

Why does the ADD CONSTRAINT looks like a machine generated string, "FKr38us2n8g5p9rj0b494sd3391"? Shouldn't it be created manually with a more meaningful constraint?

当然,约束不是由数据库生成的。它由 Hibernate 生成。

这个奇怪的名字 FKr38us2n8g5p9rj0b494sd3391 是由于某些数据库中的约束名称长度限制而产生的。例如,在 Oracle 数据库中,它不能超过 30 个符号长度。

Hibernate 通过连接表和属性名称生成约束名称并将结果转换为 MD5,生成类似 FKr38us2n8g5p9rj0b494sd3391 的字符串。此代码片段来自 Hibernate 源代码

/**
* Hash a constraint name using MD5. Convert the MD5 digest to base 35
* (full alphanumeric), guaranteeing
* that the length of the name will always be smaller than the 30
* character identifier restriction enforced by a few dialects.
*
* @param s The name to be hashed.
*
* @return String The hashed name.
*/
public String hashedName(String s) {
try {
MessageDigest md = MessageDigest.getInstance( "MD5" );
md.reset();
md.update( s.getBytes() );
byte[] digest = md.digest();
BigInteger bigInt = new BigInteger( 1, digest );
// By converting to base 35 (full alphanumeric), we guarantee
// that the length of the name will always be smaller than the 30
// character identifier restriction enforced by a few dialects.
return bigInt.toString( 35 );
}
catch ( NoSuchAlgorithmException e ) {
throw new HibernateException( "Unable to generate a hashed name!", e );
}
}

您可以使用 ImplicitNamingStrategy 生成您自己的约束名称(唯一和外键)。可以引用Hibernate5NamingStrategy , 例如。

还可以使用 @ForeignKey@JoinColumn 注释来指定约束名称(在常见情况下)。

在连接表的情况下,您可以使用 @JoinTable 注释,正如@uditkhare 所建议的。但是你需要提供更多的信息(不是很方便)

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name="person_phones",
joinColumns = @JoinColumn(name = "f_person_pid", foreignKey = @ForeignKey(
name = "fk_person_phones_person")),
inverseJoinColumns = @JoinColumn(name = "fk_phone",
foreignKey = @ForeignKey(name = "fk_person_phones_phone")),
uniqueConstraints = @UniqueConstraint(name = "uk_person_phones_phone",
columnNames = { "fk_phone" }))
private List<Phone> phones = new ArrayList<>();

Is the ADD CONSTRAINT optional in this case?

没有。它不是可选的。需要对 @OneToMany 关系建模。

关于java - 在 Hibernate 教程中添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35789480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com