【发布时间】:2018-07-23 02:43:21
【问题描述】:
@Entity
public class A {
@GeneratedValue
@Id
private long id;
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
@OneToMany(mappedBy = "a")
List<B> bs;
public List<B> getBs() {
return bs;
}
public void setBs(final List<B> bs) {
this.bs = bs;
}
}
@Entity
public class B {
@GeneratedValue
@Id
private long id;
public long getId() {
return id;
}
public void setId(final long id) {
this.id = id;
}
@ManyToOne
@JoinTable
A a;
public A getA() {
return a;
}
public void setA(final A a) {
this.a = a;
}
}
要建立关系,我必须打电话
b.setA(a);
a.getBs().add(b);
为什么两者都是必要的,为什么只做是不够的
b.setA(a);
或
a.getBs().add(b);
?
关系存储在连接表中,b.setA(a) 将更新该连接表。
但是当我之后进行查询时,a.getBs() 是空的。这是为什么呢?
这是一个说明问题的测试用例。请注意,最后一个断言失败。
public class QuickTestAB2 {
private static String dbUrlBase = "jdbc:derby:testData/db/test.db";
private static String dbUrlCreate = dbUrlBase + ";create=true";
private static String dbUrlDrop = dbUrlBase + ";drop=true";
private EntityManagerFactory factory;
private EntityManager em;
public Map<String, String> createPersistenceMap(final String dbUrl) {
final Map<String, String> persistenceMap = new HashMap<>();
persistenceMap.put("javax.persistence.jdbc.url", dbUrl);
return persistenceMap;
}
public void dropDatabase() throws Exception {
if (em != null && em.isOpen()) {
em.close();
}
if (factory != null && factory.isOpen()) {
factory.close();
}
try (Connection conn = DriverManager.getConnection(dbUrlDrop)) {
} catch (final SQLException e) {
// always
}
}
public void deleteDatabase() throws Exception {
dropDatabase();
final File file = new File("testData/db/test.db");
if (file.exists()) {
FileUtils.forceDelete(file);
}
}
public void createNewDatabase() throws SQLException, IOException {
FileUtils.forceMkdir(new File("testData/db"));
try (Connection conn = DriverManager.getConnection(dbUrlCreate)) {
}
}
@BeforeClass
public static void setUpBeforeClass01() throws Exception {
Tests.enableLog4J();
JPATests.enableJPA();
}
@AfterClass
public static void tearDownAfterClass01() throws Exception {
}
@Before
public void setUp01() throws Exception {
deleteDatabase();
createNewDatabase();
final Map<String, String> map = createPersistenceMap(dbUrlCreate);
factory = Persistence.createEntityManagerFactory("pu", map);
}
@After
public void tearDown01() throws Exception {
if (em != null && em.isOpen()) {
em.close();
}
em = null;
if (factory != null && factory.isOpen()) {
factory.close();
}
factory = null;
}
@Test
public void test01() throws Exception {
em = factory.createEntityManager();
final A a = new A();
final B b = new B();
b.setA(a);
try {
em.getTransaction().begin();
em.persist(a);
em.persist(b);
em.getTransaction().commit();
} finally {
em.close();
}
em = factory.createEntityManager();
B b2;
A a2;
try {
em.getTransaction().begin();
Query q = em.createQuery("SELECT b FROM B b");
b2 = (B) q.getSingleResult();
q = em.createQuery("SELECT a FROM A a");
a2 = (A) q.getSingleResult();
em.getTransaction().commit();
} finally {
em.close();
}
assertThat(a2, is(not(nullValue())));
assertThat(b2, is(not(nullValue())));
assertThat(b2.getA(), is(not(nullValue())));
assertThat(a2.getBs().isEmpty(), is(false));
}
}
动机:当a.Bs 的数量变大时,通过更改“仅一侧”来更改双向关系会很有用。在这种情况下,拥有方的UPDATE SELECT 查询比调用a.getBs().remove(b) 快得多
另见here。
【问题讨论】:
标签: java jpa eclipselink derby