gpt4 book ai didi

java - hibernate @OneToOne @NotNull

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:59 24 4
gpt4 key购买 nike

在关系的两边声明@OneToOne@NotNull是否有效,例如:

class ChangeEntry
{
@OneToOne(cascade=CascadeType.ALL)
@NotNull
ChangeEntryDetails changeEntryDetails;

public void addDetails(ChangeEntryDetails details) {
this.changeEntryDetails = details;
details.setChangeEntry(this);
}
}

class ChangeEntryDetails
{
@OneToOne(cascase=CascadeType.ALL)
@NotNull
ChangeEntry changeEntry;

public void setChangeEntry(ChangeEntry changeEntry)
{
this.changeEntry = changeEntry;
}
}

我找不到任何说这是无效的,但似乎在坚持期间至少必须违反关系的一侧。 (例如,如果先写入 changeEntry,则 changeEntryDetails 将暂时为 null)。

尝试此操作时,我看到抛出异常 not-null property references a null or transient value

我想尽可能避免放松约束,因为双方必须都在场。

最佳答案

Is it valid to declare @OneToOne and @NotNull on both sides of a relationship (...) I can't find anything that says this is invalid, but it seems that during persistence at least one side of the relationship must be violated. (e.g. if writing changeEntry first, changeEntryDetails will be null temporarily).

它是有效的,并且在正确映射的实体下一切正常。您需要将双向关联的一侧声明为“拥有”一侧(此“控制”插入顺序)。一种可能的工作解决方案:

@Entity
@NamedQueries( { @NamedQuery(name = ChangeEntry.FIND_ALL_CHANGEENTRIES, query = "SELECT c FROM ChangeEntry c") })
public class ChangeEntry implements Serializable {
public final static String FIND_ALL_CHANGEENTRIES = "findAllChangeEntries";

@Id
@GeneratedValue
private Long id;

@OneToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "DETAILS_ID", unique = true, nullable = false)
@NotNull
private ChangeEntryDetails changeEntryDetails;

public void addDetails(ChangeEntryDetails details) {
this.changeEntryDetails = details;
details.setChangeEntry(this);
}

// constructor, getters and setters
}

对于另一个实体(注意在关联的非拥有方设置的 mappedBy 属性):

@Entity
public class ChangeEntryDetails implements Serializable {
@Id
@GeneratedValue
private Long id;

@OneToOne(optional = false, mappedBy = "changeEntryDetails")
@NotNull
private ChangeEntry changeEntry;

// constructor, getters and setters
}

对于这些实体,以下测试(用于演示目的)通过:

public class ChangeEntryTest {
private static EntityManagerFactory emf;
private EntityManager em;

@BeforeClass
public static void createEntityManagerFactory() {
emf = Persistence.createEntityManagerFactory("TestPu");
}
@AfterClass
public static void closeEntityManagerFactory() {
emf.close();
}
@Before
public void beginTransaction() {
em = emf.createEntityManager();
em.getTransaction().begin();
}
@After
public void rollbackTransaction() {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if (em.isOpen()) {
em.close();
}
}

@Test
public void testCreateEntryWithoutDetails() {
try {
ChangeEntry entry = new ChangeEntry();
em.persist(entry);
fail("Expected ConstraintViolationException wasn't thrown.");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
ConstraintViolation<?> violation = e.getConstraintViolations()
.iterator().next();

assertEquals("changeEntryDetails", violation.getPropertyPath()
.toString());
assertEquals(NotNull.class, violation.getConstraintDescriptor()
.getAnnotation().annotationType());
}
}

@Test
public void testCreateDetailsWithoutEntry() {
try {
ChangeEntryDetails details = new ChangeEntryDetails();
em.persist(details);
fail("Expected ConstraintViolationException wasn't thrown.");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
ConstraintViolation<?> violation = e.getConstraintViolations()
.iterator().next();

assertEquals("changeEntry", violation.getPropertyPath()
.toString());
assertEquals(NotNull.class, violation.getConstraintDescriptor()
.getAnnotation().annotationType());
}
}

@Test
public void validEntryWithDetails() {
ChangeEntry entry = new ChangeEntry();
ChangeEntryDetails details = new ChangeEntryDetails();
entry.addDetails(details);
em.persist(entry);

Query query = em.createNamedQuery(ChangeEntry.FIND_ALL_CHANGEENTRIES);
assertEquals(1, query.getResultList().size());
}
}

关于java - hibernate @OneToOne @NotNull,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2725111/

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