/** * To-many relationship, resolved on first access (and after reset). Changes to to-many relations * are not persisted, make changes to the target entity. */ public List<Message> getMessages() { if (messages == null) { if (daoSession == null) { throw new DaoException("Entity is detached from DAO context"); } MessageDao targetDao = daoSession.getMessageDao(); List<Message> messagesNew = targetDao._queryConversation_Messages(id); synchronized (this) { if (messages == null) { messages = messagesNew; } } } return messages; }
@Test public void testAllMessages() throws Exception { // initially there should be no messages List<String> messages = dao.allMessages(); Assert.assertTrue(messages.isEmpty()); // now we add a message String msg = "test"; // use JdbcTemplate to store the message JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.update("insert into messages(message) values(?)", msg); messages = dao.allMessages(); Assert.assertNotNull(messages); Assert.assertEquals(messages.get(0), msg); }
@Test public void testSaveMessage() throws Exception { String msg = "test"; dao.saveMessage(msg); // use JdbcTemplate to store the message JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); List<Map<String, Object>> rows = jdbcTemplate.queryForList("select message from messages"); Map<String, Object> row = rows.get(0); String data = (String) row.get("message"); Assert.assertEquals(msg, data); }