Skip to content

Commit c1933c6

Browse files
lor6pivovarit
authored andcommitted
ormlite example (eugenp#2722)
1 parent 65aeba9 commit c1933c6

7 files changed

Lines changed: 352 additions & 0 deletions

File tree

‎libraries-data/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
<artifactId>reladomo-test-util</artifactId>
3737
<version>${reladomo.version}</version>
3838
</dependency>
39+
<dependency>
40+
<groupId>com.j256.ormlite</groupId>
41+
<artifactId>ormlite-jdbc</artifactId>
42+
<version>${ormlite.version}</version>
43+
</dependency>
3944
</dependencies>
4045

4146
<build>
@@ -144,5 +149,6 @@
144149
<reladomo.version>16.5.1</reladomo.version>
145150
<junit.version>4.12</junit.version>
146151
<maven-compiler-plugin.version>3.6.2</maven-compiler-plugin.version>
152+
<ormlite.version>5.0</ormlite.version>
147153
</properties>
148154
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.ormlite;
2+
3+
import com.j256.ormlite.field.DatabaseField;
4+
import com.j256.ormlite.table.DatabaseTable;
5+
6+
@DatabaseTable(tableName = "addresses")
7+
public class Address {
8+
@DatabaseField(generatedId = true)
9+
private long addressId;
10+
11+
@DatabaseField(canBeNull = false)
12+
private String addressLine;
13+
14+
public Address() {
15+
}
16+
17+
public Address(String addressLine) {
18+
this.addressLine = addressLine;
19+
}
20+
21+
public long getAddressId() {
22+
return addressId;
23+
}
24+
25+
public void setAddressId(long addressId) {
26+
this.addressId = addressId;
27+
}
28+
29+
public String getAddressLine() {
30+
return addressLine;
31+
}
32+
33+
public void setAddressLine(String addressLine) {
34+
this.addressLine = addressLine;
35+
}
36+
37+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.ormlite;
2+
3+
import com.j256.ormlite.field.DatabaseField;
4+
import com.j256.ormlite.table.DatabaseTable;
5+
6+
@DatabaseTable
7+
public class Book {
8+
9+
@DatabaseField(generatedId = true)
10+
private long bookId;
11+
12+
@DatabaseField
13+
private String title;
14+
15+
@DatabaseField(foreign = true, foreignAutoRefresh = true, foreignAutoCreate = true)
16+
private Library library;
17+
18+
public Book() {
19+
}
20+
21+
public Book(String title) {
22+
this.title = title;
23+
}
24+
25+
public long getBookId() {
26+
return bookId;
27+
}
28+
29+
public void setBookId(long bookId) {
30+
this.bookId = bookId;
31+
}
32+
33+
public String getTitle() {
34+
return title;
35+
}
36+
37+
public void setTitle(String title) {
38+
this.title = title;
39+
}
40+
41+
public Library getLibrary() {
42+
return library;
43+
}
44+
45+
public void setLibrary(Library library) {
46+
this.library = library;
47+
}
48+
49+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.ormlite;
2+
3+
import com.j256.ormlite.dao.ForeignCollection;
4+
import com.j256.ormlite.field.DatabaseField;
5+
import com.j256.ormlite.field.ForeignCollectionField;
6+
import com.j256.ormlite.table.DatabaseTable;
7+
8+
@DatabaseTable(tableName = "libraries", daoClass = LibraryDaoImpl.class)
9+
public class Library {
10+
11+
@DatabaseField(generatedId = true)
12+
private long libraryId;
13+
14+
@DatabaseField(canBeNull = false)
15+
private String name;
16+
17+
@DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
18+
private Address address;
19+
20+
@ForeignCollectionField(eager = false)
21+
private ForeignCollection<Book> books;
22+
23+
public Library() {
24+
}
25+
26+
public long getLibraryId() {
27+
return libraryId;
28+
}
29+
30+
public void setLibraryId(long libraryId) {
31+
this.libraryId = libraryId;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public Address getAddress() {
43+
return address;
44+
}
45+
46+
public void setAddress(Address address) {
47+
this.address = address;
48+
}
49+
50+
public ForeignCollection<Book> getBooks() {
51+
return books;
52+
}
53+
54+
public void setBooks(ForeignCollection<Book> books) {
55+
this.books = books;
56+
}
57+
58+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.ormlite;
2+
3+
import java.sql.SQLException;
4+
import java.util.List;
5+
6+
import com.j256.ormlite.dao.Dao;
7+
8+
public interface LibraryDao extends Dao<Library, Long> {
9+
public List<Library> findByName(String name) throws SQLException;
10+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.ormlite;
2+
3+
import java.sql.SQLException;
4+
import java.util.List;
5+
6+
import com.j256.ormlite.dao.BaseDaoImpl;
7+
import com.j256.ormlite.support.ConnectionSource;
8+
9+
public class LibraryDaoImpl extends BaseDaoImpl<Library, Long> implements LibraryDao {
10+
11+
public LibraryDaoImpl(ConnectionSource connectionSource) throws SQLException {
12+
super(connectionSource, Library.class);
13+
}
14+
15+
@Override
16+
public List<Library> findByName(String name) throws SQLException {
17+
return super.queryForEq("name", name);
18+
19+
}
20+
21+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package com.baeldung.ormlite;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.IOException;
6+
import java.sql.SQLException;
7+
import java.util.List;
8+
9+
import org.junit.After;
10+
import org.junit.AfterClass;
11+
import org.junit.BeforeClass;
12+
import org.junit.Test;
13+
import com.j256.ormlite.dao.CloseableWrappedIterable;
14+
import com.j256.ormlite.dao.Dao;
15+
import com.j256.ormlite.dao.DaoManager;
16+
import com.j256.ormlite.jdbc.JdbcPooledConnectionSource;
17+
import com.j256.ormlite.table.TableUtils;
18+
19+
public class ORMLiteTest {
20+
private static JdbcPooledConnectionSource connectionSource;
21+
22+
private static Dao<Library, Long> libraryDao;
23+
private static Dao<Book, Long> bookDao;
24+
25+
@BeforeClass
26+
public static void setup() throws SQLException {
27+
connectionSource = new JdbcPooledConnectionSource("jdbc:h2:mem:myDb");
28+
TableUtils.createTableIfNotExists(connectionSource, Library.class);
29+
TableUtils.createTableIfNotExists(connectionSource, Address.class);
30+
TableUtils.createTableIfNotExists(connectionSource, Book.class);
31+
32+
libraryDao = DaoManager.createDao(connectionSource, Library.class);
33+
34+
bookDao = DaoManager.createDao(connectionSource, Book.class);
35+
}
36+
37+
@Test
38+
public void givenDAO_whenCRUD_thenOk() throws SQLException {
39+
Library library = new Library();
40+
library.setName("My Library");
41+
libraryDao.create(library);
42+
43+
Library result = libraryDao.queryForId(library.getLibraryId());
44+
assertEquals("My Library", result.getName());
45+
46+
library.setName("My Other Library");
47+
libraryDao.update(library);
48+
49+
libraryDao.delete(library);
50+
51+
}
52+
53+
@Test
54+
public void whenLoopDao_thenOk() throws SQLException {
55+
Library library1 = new Library();
56+
library1.setName("My Library");
57+
libraryDao.create(library1);
58+
59+
Library library2 = new Library();
60+
library2.setName("My Other Library");
61+
libraryDao.create(library2);
62+
63+
libraryDao.forEach(lib -> {
64+
System.out.println(lib.getName());
65+
});
66+
67+
}
68+
69+
@Test
70+
public void givenIterator_whenLoop_thenOk() throws SQLException, IOException {
71+
Library library1 = new Library();
72+
library1.setName("My Library");
73+
libraryDao.create(library1);
74+
75+
Library library2 = new Library();
76+
library2.setName("My Other Library");
77+
libraryDao.create(library2);
78+
79+
CloseableWrappedIterable<Library> wrappedIterable = libraryDao.getWrappedIterable();
80+
try {
81+
wrappedIterable.forEach(lib -> {
82+
System.out.println(lib.getName());
83+
});
84+
} finally {
85+
wrappedIterable.close();
86+
}
87+
88+
}
89+
90+
@Test
91+
public void givenCustomDao_whenSave_thenOk() throws SQLException, IOException {
92+
Library library = new Library();
93+
library.setName("My Library");
94+
95+
LibraryDao customLibraryDao = DaoManager.createDao(connectionSource, Library.class);
96+
customLibraryDao.create(library);
97+
assertEquals(1, customLibraryDao.findByName("My Library")
98+
.size());
99+
}
100+
101+
@Test
102+
public void whenSaveForeignField_thenOk() throws SQLException, IOException {
103+
Library library = new Library();
104+
library.setName("My Library");
105+
library.setAddress(new Address("Main Street nr 20"));
106+
libraryDao.create(library);
107+
108+
Dao<Address, Long> addressDao = DaoManager.createDao(connectionSource, Address.class);
109+
assertEquals(1, addressDao.queryForEq("addressLine", "Main Street nr 20")
110+
.size());
111+
}
112+
113+
@Test
114+
public void whenSaveForeignCollection_thenOk() throws SQLException, IOException {
115+
Library library = new Library();
116+
library.setName("My Library");
117+
libraryDao.create(library);
118+
libraryDao.refresh(library);
119+
library.getBooks()
120+
.add(new Book("1984"));
121+
122+
Book book = new Book("It");
123+
book.setLibrary(library);
124+
bookDao.create(book);
125+
126+
assertEquals(2, bookDao.queryForEq("library_id", library)
127+
.size());
128+
}
129+
130+
@Test
131+
public void whenGetLibrariesWithMoreThanOneBook_thenOk() throws SQLException, IOException {
132+
Library library = new Library();
133+
library.setName("My Library");
134+
libraryDao.create(library);
135+
Library library2 = new Library();
136+
library2.setName("My Other Library");
137+
libraryDao.create(library2);
138+
139+
libraryDao.refresh(library);
140+
libraryDao.refresh(library2);
141+
142+
library.getBooks()
143+
.add(new Book("Book1"));
144+
library2.getBooks()
145+
.add(new Book("Book2"));
146+
library2.getBooks()
147+
.add(new Book("Book3"));
148+
149+
List<Library> libraries = libraryDao.queryBuilder()
150+
.where()
151+
.in("libraryId", bookDao.queryBuilder()
152+
.selectColumns("library_id")
153+
.groupBy("library_id")
154+
.having("count(*) > 1"))
155+
.query();
156+
assertEquals(1, libraries.size());
157+
158+
}
159+
160+
@After
161+
public void clear() throws SQLException {
162+
TableUtils.clearTable(connectionSource, Library.class);
163+
TableUtils.clearTable(connectionSource, Book.class);
164+
TableUtils.clearTable(connectionSource, Address.class);
165+
}
166+
167+
@AfterClass
168+
public static void tearDown() throws SQLException, IOException {
169+
connectionSource.close();
170+
}
171+
}

0 commit comments

Comments
 (0)