ormlite example (#2722)
This commit is contained in:
parent
65aeba9d3d
commit
c1933c6819
@ -36,6 +36,11 @@
|
|||||||
<artifactId>reladomo-test-util</artifactId>
|
<artifactId>reladomo-test-util</artifactId>
|
||||||
<version>${reladomo.version}</version>
|
<version>${reladomo.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.j256.ormlite</groupId>
|
||||||
|
<artifactId>ormlite-jdbc</artifactId>
|
||||||
|
<version>${ormlite.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -144,5 +149,6 @@
|
|||||||
<reladomo.version>16.5.1</reladomo.version>
|
<reladomo.version>16.5.1</reladomo.version>
|
||||||
<junit.version>4.12</junit.version>
|
<junit.version>4.12</junit.version>
|
||||||
<maven-compiler-plugin.version>3.6.2</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.6.2</maven-compiler-plugin.version>
|
||||||
|
<ormlite.version>5.0</ormlite.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.ormlite;
|
||||||
|
|
||||||
|
import com.j256.ormlite.field.DatabaseField;
|
||||||
|
import com.j256.ormlite.table.DatabaseTable;
|
||||||
|
|
||||||
|
@DatabaseTable(tableName = "addresses")
|
||||||
|
public class Address {
|
||||||
|
@DatabaseField(generatedId = true)
|
||||||
|
private long addressId;
|
||||||
|
|
||||||
|
@DatabaseField(canBeNull = false)
|
||||||
|
private String addressLine;
|
||||||
|
|
||||||
|
public Address() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address(String addressLine) {
|
||||||
|
this.addressLine = addressLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getAddressId() {
|
||||||
|
return addressId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddressId(long addressId) {
|
||||||
|
this.addressId = addressId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddressLine() {
|
||||||
|
return addressLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddressLine(String addressLine) {
|
||||||
|
this.addressLine = addressLine;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
49
libraries-data/src/main/java/com/baeldung/ormlite/Book.java
Normal file
49
libraries-data/src/main/java/com/baeldung/ormlite/Book.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.ormlite;
|
||||||
|
|
||||||
|
import com.j256.ormlite.field.DatabaseField;
|
||||||
|
import com.j256.ormlite.table.DatabaseTable;
|
||||||
|
|
||||||
|
@DatabaseTable
|
||||||
|
public class Book {
|
||||||
|
|
||||||
|
@DatabaseField(generatedId = true)
|
||||||
|
private long bookId;
|
||||||
|
|
||||||
|
@DatabaseField
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@DatabaseField(foreign = true, foreignAutoRefresh = true, foreignAutoCreate = true)
|
||||||
|
private Library library;
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getBookId() {
|
||||||
|
return bookId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBookId(long bookId) {
|
||||||
|
this.bookId = bookId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Library getLibrary() {
|
||||||
|
return library;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLibrary(Library library) {
|
||||||
|
this.library = library;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
package com.baeldung.ormlite;
|
||||||
|
|
||||||
|
import com.j256.ormlite.dao.ForeignCollection;
|
||||||
|
import com.j256.ormlite.field.DatabaseField;
|
||||||
|
import com.j256.ormlite.field.ForeignCollectionField;
|
||||||
|
import com.j256.ormlite.table.DatabaseTable;
|
||||||
|
|
||||||
|
@DatabaseTable(tableName = "libraries", daoClass = LibraryDaoImpl.class)
|
||||||
|
public class Library {
|
||||||
|
|
||||||
|
@DatabaseField(generatedId = true)
|
||||||
|
private long libraryId;
|
||||||
|
|
||||||
|
@DatabaseField(canBeNull = false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
|
||||||
|
private Address address;
|
||||||
|
|
||||||
|
@ForeignCollectionField(eager = false)
|
||||||
|
private ForeignCollection<Book> books;
|
||||||
|
|
||||||
|
public Library() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getLibraryId() {
|
||||||
|
return libraryId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLibraryId(long libraryId) {
|
||||||
|
this.libraryId = libraryId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(Address address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ForeignCollection<Book> getBooks() {
|
||||||
|
return books;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBooks(ForeignCollection<Book> books) {
|
||||||
|
this.books = books;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.ormlite;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.j256.ormlite.dao.Dao;
|
||||||
|
|
||||||
|
public interface LibraryDao extends Dao<Library, Long> {
|
||||||
|
public List<Library> findByName(String name) throws SQLException;
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.ormlite;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.j256.ormlite.dao.BaseDaoImpl;
|
||||||
|
import com.j256.ormlite.support.ConnectionSource;
|
||||||
|
|
||||||
|
public class LibraryDaoImpl extends BaseDaoImpl<Library, Long> implements LibraryDao {
|
||||||
|
|
||||||
|
public LibraryDaoImpl(ConnectionSource connectionSource) throws SQLException {
|
||||||
|
super(connectionSource, Library.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Library> findByName(String name) throws SQLException {
|
||||||
|
return super.queryForEq("name", name);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,171 @@
|
|||||||
|
package com.baeldung.ormlite;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
import com.j256.ormlite.dao.CloseableWrappedIterable;
|
||||||
|
import com.j256.ormlite.dao.Dao;
|
||||||
|
import com.j256.ormlite.dao.DaoManager;
|
||||||
|
import com.j256.ormlite.jdbc.JdbcPooledConnectionSource;
|
||||||
|
import com.j256.ormlite.table.TableUtils;
|
||||||
|
|
||||||
|
public class ORMLiteTest {
|
||||||
|
private static JdbcPooledConnectionSource connectionSource;
|
||||||
|
|
||||||
|
private static Dao<Library, Long> libraryDao;
|
||||||
|
private static Dao<Book, Long> bookDao;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setup() throws SQLException {
|
||||||
|
connectionSource = new JdbcPooledConnectionSource("jdbc:h2:mem:myDb");
|
||||||
|
TableUtils.createTableIfNotExists(connectionSource, Library.class);
|
||||||
|
TableUtils.createTableIfNotExists(connectionSource, Address.class);
|
||||||
|
TableUtils.createTableIfNotExists(connectionSource, Book.class);
|
||||||
|
|
||||||
|
libraryDao = DaoManager.createDao(connectionSource, Library.class);
|
||||||
|
|
||||||
|
bookDao = DaoManager.createDao(connectionSource, Book.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDAO_whenCRUD_thenOk() throws SQLException {
|
||||||
|
Library library = new Library();
|
||||||
|
library.setName("My Library");
|
||||||
|
libraryDao.create(library);
|
||||||
|
|
||||||
|
Library result = libraryDao.queryForId(library.getLibraryId());
|
||||||
|
assertEquals("My Library", result.getName());
|
||||||
|
|
||||||
|
library.setName("My Other Library");
|
||||||
|
libraryDao.update(library);
|
||||||
|
|
||||||
|
libraryDao.delete(library);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenLoopDao_thenOk() throws SQLException {
|
||||||
|
Library library1 = new Library();
|
||||||
|
library1.setName("My Library");
|
||||||
|
libraryDao.create(library1);
|
||||||
|
|
||||||
|
Library library2 = new Library();
|
||||||
|
library2.setName("My Other Library");
|
||||||
|
libraryDao.create(library2);
|
||||||
|
|
||||||
|
libraryDao.forEach(lib -> {
|
||||||
|
System.out.println(lib.getName());
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenIterator_whenLoop_thenOk() throws SQLException, IOException {
|
||||||
|
Library library1 = new Library();
|
||||||
|
library1.setName("My Library");
|
||||||
|
libraryDao.create(library1);
|
||||||
|
|
||||||
|
Library library2 = new Library();
|
||||||
|
library2.setName("My Other Library");
|
||||||
|
libraryDao.create(library2);
|
||||||
|
|
||||||
|
CloseableWrappedIterable<Library> wrappedIterable = libraryDao.getWrappedIterable();
|
||||||
|
try {
|
||||||
|
wrappedIterable.forEach(lib -> {
|
||||||
|
System.out.println(lib.getName());
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
wrappedIterable.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCustomDao_whenSave_thenOk() throws SQLException, IOException {
|
||||||
|
Library library = new Library();
|
||||||
|
library.setName("My Library");
|
||||||
|
|
||||||
|
LibraryDao customLibraryDao = DaoManager.createDao(connectionSource, Library.class);
|
||||||
|
customLibraryDao.create(library);
|
||||||
|
assertEquals(1, customLibraryDao.findByName("My Library")
|
||||||
|
.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSaveForeignField_thenOk() throws SQLException, IOException {
|
||||||
|
Library library = new Library();
|
||||||
|
library.setName("My Library");
|
||||||
|
library.setAddress(new Address("Main Street nr 20"));
|
||||||
|
libraryDao.create(library);
|
||||||
|
|
||||||
|
Dao<Address, Long> addressDao = DaoManager.createDao(connectionSource, Address.class);
|
||||||
|
assertEquals(1, addressDao.queryForEq("addressLine", "Main Street nr 20")
|
||||||
|
.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSaveForeignCollection_thenOk() throws SQLException, IOException {
|
||||||
|
Library library = new Library();
|
||||||
|
library.setName("My Library");
|
||||||
|
libraryDao.create(library);
|
||||||
|
libraryDao.refresh(library);
|
||||||
|
library.getBooks()
|
||||||
|
.add(new Book("1984"));
|
||||||
|
|
||||||
|
Book book = new Book("It");
|
||||||
|
book.setLibrary(library);
|
||||||
|
bookDao.create(book);
|
||||||
|
|
||||||
|
assertEquals(2, bookDao.queryForEq("library_id", library)
|
||||||
|
.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenGetLibrariesWithMoreThanOneBook_thenOk() throws SQLException, IOException {
|
||||||
|
Library library = new Library();
|
||||||
|
library.setName("My Library");
|
||||||
|
libraryDao.create(library);
|
||||||
|
Library library2 = new Library();
|
||||||
|
library2.setName("My Other Library");
|
||||||
|
libraryDao.create(library2);
|
||||||
|
|
||||||
|
libraryDao.refresh(library);
|
||||||
|
libraryDao.refresh(library2);
|
||||||
|
|
||||||
|
library.getBooks()
|
||||||
|
.add(new Book("Book1"));
|
||||||
|
library2.getBooks()
|
||||||
|
.add(new Book("Book2"));
|
||||||
|
library2.getBooks()
|
||||||
|
.add(new Book("Book3"));
|
||||||
|
|
||||||
|
List<Library> libraries = libraryDao.queryBuilder()
|
||||||
|
.where()
|
||||||
|
.in("libraryId", bookDao.queryBuilder()
|
||||||
|
.selectColumns("library_id")
|
||||||
|
.groupBy("library_id")
|
||||||
|
.having("count(*) > 1"))
|
||||||
|
.query();
|
||||||
|
assertEquals(1, libraries.size());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void clear() throws SQLException {
|
||||||
|
TableUtils.clearTable(connectionSource, Library.class);
|
||||||
|
TableUtils.clearTable(connectionSource, Book.class);
|
||||||
|
TableUtils.clearTable(connectionSource, Address.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void tearDown() throws SQLException, IOException {
|
||||||
|
connectionSource.close();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user