ormlite example (#2722)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user