在以下装修和条件下,
/**
* passsalt.
*/
@JoinColumn(name = "PASSSALT_ID", nullable = false)
@OneToOne(cascade = {CascadeType.PERSIST,
CascadeType.MERGE, // is this actually required?
CascadeType.REMOVE},
optional = false, orphanRemoval = true)
@NotNull
@XmlTransient
private Morton passsalt;
passsalt
必须与该实体一起存在。(CascadeType.PERSIST
)passsalt
可以用新detached
实例替换。(CascadeType.MERGE
?)passsalt
删除此实体时必须删除。(CascadeType.REMOVE
)Morton
没有要更新的字段
问题:CascadeType.MERGE
是强制性的吗?
我在有无的情况下进行了测试,没有CascadeType.MERGE
它似乎也可以工作。
Q2:问题标题中的“是”是否正确?应该是“是”吗?
这是一种替换的方法passsalt
。
public boolean nassword(final Shadow reference, final byte[] password,
final byte[] nassword) {
passsalt = new Morton();
passcode = passsalt.salty(nassword);
return true;
}
我用以下方法进行了测试。
@Test(enabled = true, invocationCount = 1)
public void testNassword0() {
final EntityManager manager = LocalPU.createEntityManager();
try {
final EntityTransaction transaction = manager.getTransaction();
transaction.begin();
try {
final String username = newUsername(manager);
final byte[] password = newPassword();
Shadow shadow = persistInstance(manager, username, password);
Assert.assertTrue(shadow.puthenticate(shadow, password));
System.out.println("=========================================");
LOGGER.log(Level.INFO, "mortons: {0}",
MORTONS(manager, 0, 1024));
final byte[] nassword = newPassword();
shadow.nassword(shadow, password, nassword);
shadow = manager.merge(shadow);
manager.flush();
System.out.println("=========================================");
LOGGER.log(Level.INFO, "mortons: {0}",
MORTONS(manager, 0, 1024));
Assert.assertFalse(shadow.puthenticate(shadow, password));
Assert.assertTrue(shadow.puthenticate(shadow, nassword));
transaction.commit();
} catch (Exception e) {
transaction.rollback();
e.printStackTrace(System.err);
Assert.fail(e.getMessage());
}
} finally {
manager.close();
}
}
这是输出。
4월 02, 2013 11:55:03 오전 org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 4.3.0.Final
[EL Info]: 2013-04-02 11:55:03.897--ServerSession(571229670)--EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
[EL Info]: 2013-04-02 11:55:04.744--ServerSession(571229670)--file:/E:/svnwc/jinahya.googlecode.com/trunk/com.googlecode.jinahya/jinahya-ee/target/test-classes/_localPU login successful
4월 02, 2013 11:55:06 오전 com.googlecode.jinahya.persistence.Morton _PrePersist
INFO: _PrePersist(): Morton@1760972179&id=1048576
4월 02, 2013 11:55:06 오전 com.googlecode.jinahya.persistence.Shadow puthenticate
INFO: puthenticate(Shadow@1311874806?id=1048576&username=0KIWHFirmA581Qf5AscsrVbN9YW30pAF&passcode=[B@57d01cc, [B@5200fae4)
4월 02, 2013 11:55:06 오전 com.googlecode.jinahya.persistence.Shadow puthenticate
INFO: passsalt: Morton@1760972179&id=1048576
=========================================
4월 02, 2013 11:55:08 오전 com.googlecode.jinahya.persistence.ShadowTest testNassword0
INFO: mortons: [Morton@1760972179&id=1048576]
4월 02, 2013 11:55:08 오전 com.googlecode.jinahya.persistence.Shadow nassword
INFO: nassword(Shadow@1311874806?id=1048576&username=0KIWHFirmA581Qf5AscsrVbN9YW30pAF&passcode=[B@57d01cc, [B@5200fae4, [B@1b6ac76b)
4월 02, 2013 11:55:08 오전 com.googlecode.jinahya.persistence.Shadow puthenticate
INFO: puthenticate(Shadow@1311874806?id=1048576&username=0KIWHFirmA581Qf5AscsrVbN9YW30pAF&passcode=[B@57d01cc, [B@5200fae4)
4월 02, 2013 11:55:08 오전 com.googlecode.jinahya.persistence.Shadow puthenticate
INFO: passsalt: Morton@1760972179&id=1048576
4월 02, 2013 11:55:11 오전 com.googlecode.jinahya.persistence.Morton _PrePersist
INFO: _PrePersist(): Morton@93935309&id=1048577
=========================================
4월 02, 2013 11:55:11 오전 com.googlecode.jinahya.persistence.ShadowTest testNassword0
INFO: mortons: [Morton@93935309&id=1048577]
4월 02, 2013 11:55:11 오전 com.googlecode.jinahya.persistence.Shadow puthenticate
INFO: puthenticate(Shadow@1311874806?id=1048576&username=0KIWHFirmA581Qf5AscsrVbN9YW30pAF&passcode=[B@641e8314, [B@5200fae4)
4월 02, 2013 11:55:11 오전 com.googlecode.jinahya.persistence.Shadow puthenticate
INFO: passsalt: Morton@93935309&id=1048577
4월 02, 2013 11:55:12 오전 com.googlecode.jinahya.persistence.Shadow puthenticate
INFO: puthenticate(Shadow@1311874806?id=1048576&username=0KIWHFirmA581Qf5AscsrVbN9YW30pAF&passcode=[B@641e8314, [B@1b6ac76b)
4월 02, 2013 11:55:12 오전 com.googlecode.jinahya.persistence.Shadow puthenticate
INFO: passsalt: Morton@93935309&id=1048577
总之,
After persisted -> mortons: [Morton@1760972179&id=1048576]
After replaced and merged -> mortons: [Morton@93935309&id=1048577]