Mybatis select with nested objects - java

I am using MyBatis to do a simple select.
Assume we have the following classes:
class Book {
private String bookName;
public Book(String bookName){
this.bookName = bookName;
}
public String getBookName(){
return bookName;
}
}
class Student {
private String studentName;
private Book book;
public Student(){}
// getters and setters
}
I have an annotation on a method that returns a Student object.
#Select("Select studentName, book from Students")
My Issue is that book is always null. I was under the assumption MyBatis will call the constructor with that JDBC type (in this case String) to populate book. What am I missing or doing incorrectly?

One option is
Use #ConstructorArgs annotations to explicitly call Constructor method.
#Select("Select studentName, book from Students")
#ConstructorArgs(value = {
#Arg(column = "studentName", javaType=java.lang.String.class),
#Arg(column = "book", javaType = java.lang.String.class)
})
and pass them to Student constructor, which calls Book constructor.

Related

How to use parent class default values in child class builder

I am having two classes:
1]BaseCustomer.java
#NoArgsConstructor
#AllArgsConstructor
#Getter
#Builder(builderMethodName="BaseBuilder")
public class BaseCusmtomer {
private String cutomerId;
private String age;
#Default
private Boolean isActive= true;
#Default
private String type = "XYZ";
}
2] Customer.java
#Builder
public class Customer extends BaseCustomer{
private Customer(String cutomerId, String age, Boolean isActive, String type){
super(customerId,age,isActive,type);
}
}
3]Test Object
Customer.builder().cutomerId("1").age("23").build();
ut while creating object using Customer builder it always take values of isActive and type as null, it should take default values from superclass. Is there anyway to do this?
Tried to call Child builder with default parent class values
but getting null values instead of default value.
Note: can't use Superbuilder as it is experimental feature.
Since using #SuperBuilder is not an option for you, there is not much to do. One option is to create BaseCustomer "copy" constructor and create Customer by passing BaseCustomer to copy . Like this:
#NoArgsConstructor
#AllArgsConstructor
#Getter
#Builder(builderMethodName = "BaseBuilder")
public class BaseCustomer {
protected String customerId;
protected String age;
#Default
protected Boolean isActive = true;
#Default
protected String type = "XYZ";
public BaseCustomer(BaseCustomer base) {
this.customerId = base.customerId;
this.age = base.getAge();
this.isActive = base.isActive;
this.type = base.type;
}
}
#Data
public class Customer extends BaseCustomer {
String name;
#Builder
private Customer(BaseCustomer base, String name) {
super(base);
this.name = name;
}
}
So as can be seen above, I marked Customer constructor with BaseCustomer parameter as #Builder. I added new parameter to Customer to see how it will work with additional fields. Now we can create Customer by building BaseCustomer first and then pass it with additional fields. For example:
Customer c2 = Customer.builder().base(BaseCustomer.BaseBuilder().customerId("1").age("23").build()).name("Name").build();
System.out.println(c2.getAge());
System.out.println(c2.getCustomerId());
System.out.println(c2.getType());
System.out.println(c2.getIsActive());
System.out.println(c2.getName());
This will print:
23
1
XYZ
true
Name
This has some advantages - you only pass single parameter (for base class) to Customer constructor and Customer constructor does not have to be changed for BaseCustomer field changes.

Return List type in JPA

I have DTO structure like :
public class ADto{
private String name;
private String id;
private List<BDto> bdtos;
//Created constructor using fields
}
public class BDto{
private String id;
private String code;
private List<CDto> cdtos;
//Created constructor using fields
}
public class CDto{
private String mKey;
private String mVal;
//Created constructor using fields
}
Used Spring MVC for fetching the data.
Below query is working perfectly fine and binding the data :
#org.springframework.data.jpa.repository.Query("select new pkg.ADto(id,name) from AEntity a where a.id=?1")
public ADto getAData(Long id);
How can I fetch the data for the list which is in turn composed of further list using the above method?
If you want to return DTOs instead on enitites, you need to provide mapping between DTOs and entities. With JPQL query, the only option is to provide that mapping in constructor of the resulting object. Therefore, you need to add a constructor to ADto, which accepts BEntities, and map all nested entities to dtos in that constructor. Or in more object oriented way, the new constructor will accept AEntity as the only argument.
This is how it could look like:
getAData() method in the repository (JPQL is slightly modified by adding a.bEntities to result):
#org.springframework.data.jpa.repository.Query("select new pkg.ADto(id,name, a.bEntities) from AEntity a where a.id=?1")
public ADto getAData(Long id);
New constructor in ADto:
public class ADto{
private String name;
private String id;
private List<BDto> bdtos;
public ADto(String id, String name, List<BEntity> bEntities) {
this.id = id; this.name = name;
this.bdtos = new ArrayList<>();
for (BEntity b : bEntities) {
BDto bdto = new BDto(b.id, b.code, b.cEntities);
/* you need to pass cEntities and map them again in the BDto
* constructor, or you may do the apping in ADto constructor
* and only pass mapped values to BDto constructor */
}
}
}
You have to enable eager fetch:
#OneToMany(mappedBy = "adto", fetch = FetchType.EAGER)
private List<BDto> bdtos;
Then you can fetch it like this i.e.:
ADto findById(Long id); // literally!

Class relationship, NullPointerException error

I have two classes of Authors and Books:
public class Authors extends RealmObject {
#PrimaryKey
private String url_base;
private RealmList<Books> books;
... getters & setters...
public RealmList<Books> getBooks() {
return books;
}
public void setBooks(RealmList<Books> books) {
this.books = books;
}
}
public class Books extends RealmObject {
#PrimaryKey
private String url_base;
private Authors author;
... getters & setters...
public Authors getAuthor() {
return author;
}
public void setAuthor(Authors author) {
this.author = author;
}
}
Perform inserts:
Authors author = new Authors();
author.setUrl_base("url_base")
Books book = new Books();
book.setUrl_base("lala");
book.setAuthor(author);
author.getBooks().add(book); // error comes here
realm.beginTransaction();
realm.copyToRealmOrUpdate(author);
realm.commitTransaction();
And the program gives java.lang.NullPointerException.
Error line : author.getBooks().Add (book);
What could be wrong? I ask your help.
You haven't set the books member for your author object (as books is not initialized it is null) so getBooks() will return a null.
Make sure books is initialized before using it :
public class Authors extends RealmObject {
#PrimaryKey
private String url_base;
private RealmList<Books> books = new RealmList<Books>(); // An empty unmanaged books list.
... getters & setters...
or use the setter method (though initializing it on construction will be more correct in this case):
Authors author = new Authors();
author.setUrl_base("url_base")
author.setBooks(new RealmList<Book>());
UPDATE:
As #AndreyAtapin correctly noted in the solution above the list will be unmanaged. If you want a managed list pass the required arguments as specified in the RealmList api but whatever you choose the books list must be initialized before you start manipulating it
According to Realm docs you should instantiate entity objects with special factory method:
realm.beginTransaction();
Authors author = realm.createObject(Authors.class);
author.setUrl_base("url_base")
Books book = realm.createObject(Books.class);
book.setUrl_base("lala");
book.setAuthor(author);
author.getBooks().add(book); // error comes here
realm.commitTransaction();
When you instantiate your entities with constructor, obviously all fields are null by default:
public class Authors extends RealmObject {
#PrimaryKey
private String url_base; // = null
private RealmList<Books> books; // = null
... getters & setters...
}
PS: By the way, using variables names url_base contradicts the Java coding conventions. You better call it urlBase (setUrlBase/getUrlBase accordingly).

Confused with the Flow of constructors

I am trying out some examples from Beginning Java EE6 with GlassFish3 .So , i created an entity class that basically looks like this ...
#Entity
#Table(name="Book")
public class Book implements Serializable
{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(nullable=false)
private String name;
#Column(nullable=false)
private String isbn;
private String description;
public Book()
{
// Empty constructor to facilitate construction.
System.out.println("The variables have not been initialized...Please initialize them using the Setters or use the provided constructor");
}
public Book(String name, String isbn, String description) {
this.name = name;
this.isbn = isbn;
this.description = description;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Override
public String toString() {
return this.name + " - " + this.isbn;
}
#PrePersist
public void printPrePersist(){
System.out.println("Persisting the book "+this.name);
}
#PostPersist
public void printPostPersist(){
System.out.println("Persisted the book "+this.name);
}
}
and i tried to persist it like this ...
public class MainClass
{
public static void main(String[] args){
Book book = new Book("Effective Java","ISBN - 1234415","A very good book on Java");
Book book2 = new Book("Learning Java EE","ISBN - 1233415","A good book for Java EE beginners");
// These are the necessary classes
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PersistenceAppPU");
EntityManager em = emf.createEntityManager();
// Persist the book here
EntityTransaction etx = em.getTransaction();
etx.begin();
em.persist(book);
em.persist(book2);
etx.commit();
em.close();
emf.close();
System.out.println("The two books have been persisted");
}
}
It persists , but when i run , i see an output like ...
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
Persisting the book Effective Java
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
Persisting the book Learning Java EE
Persisted the book Learning Java EE
Persisted the book Effective Java
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
The variables have not been initialized...Please initialize them using the Setters or use the provided constructor
[EL Info]: 2012-05-10 12:01:19.623--ServerSession(17395905)--file:/C:/Users/raviteja.s/Documents/NetBeansProjects/PersistenceApp/src/_PersistenceAppPU logout successful
The two books have been persisted
I dont understand , why there are so many default constructor calls when , there is not one made by me ... ?
Could somebody explain me how the flow is in the sample that i have ?
JPA uses a constructor with no arguments in order to instantiate your Entities, and then bind fields in those entities to the correspondent mapped tables and columns.
Those output you see are the calls that JPA does for you every time it manipulates your entities.

'dynamic'-like java annotations?

I have a pojo that is dependent on annotations. It has predefined fields as well as a Set that contains user provided fields:
public class MyPOJO implements Document {
private String id;
private LocalString name;
private LocalString desc;
private List<Field> fields;
public MyPOJO(final String id,
final LocalString name,
final LocalString desc,
final List<Field> fields) {
this.id = id;
this.name = name;
this.desc = desc;
this.fields = fields;
}
public String getId() {
return id;
}
#Indexed(searchable = false, stored = true)
public LocalString getName() {
return name;
}
#Indexed(searchable = true)
public LocalString getDescription() {
return desc;
}
public List<Field> getFields() {
return fields;
}
}
MyPOJO is a 'generic' object, ie, the developer (or consumer) of MyPOJO has fields that are not predefined in MyPOJO and therefore the developer needs to place these additional fields the in attribute 'fields'. The problem arises from the fact that each object in the Set fields needs to have its own annotations to indicate whether the particular field is either stored or searchable in order to remain consistent with the predefined attributes, such as name.
I can think of two options:
For each additional field, the developer will have to create an
anonymous class implementing the interface Field and inside this
anonymous class, the developer will declare the applicable
annotations.
the Set 'fields' contains a complex object of fieldname, fieldvalue
and annotations as shown below. I can't figure out how to invoke the constructor for Field. The below code does not compile but it is intended as pseudo-code to signify what I am trying to do.
Field myfield1 = new Field("dateofBirth", new Date(), new ArrayList({Index.stored, Index.searchable});
Field myfield2 = new Field("model", "330i", new ArrayList({Index.stored});
There is no construct to pass annotations as a parameter: new ArrayList({Index.stored}.
public class Field {
private String name;
private Object value;
Collection<Annotation> annotations;
public Field(final String name, final Object value, Collection<Annotation> annotations;) {
this.name = name;
this.value = value;
this.annotations = Collections.unmodifiableCollection(annotations);
}
public String getName() {
return name;
}
public Object getValue() {
return value;
}
}
I'm not particularly excited with either option and hoping someone can give me some pointers
If you need an extensible object model, I'd say a POJO design is just setting yourself up for extra work as opposed to exposing a metamodel.
That said, what you could do is have clients of the API subclass MyPOJO, and annotate the properties they define in their subclasses. You would then use reflection to go through all JavaBeans properties of the objects you're receiving and determine the annotations on the getters - similarly to how JPA works.

Categories

Resources