I am using Mybatis (version 3.2.7) as the ORM framework for my JAVA project. Since I am against the backdrop of the JPA, I really wanted to learn LAZYLOADING, supported by Mybatis. But I could not understand anything significant.
(I configure MYBATIS using the JAVA API and annotations are for query only)
According to Mybatis documentation: <i> <b> 1. lazyLoadingEnabled: default = TRUE
Globally enables or disables lazy loading. When enabled, all relationships will be lazy loaded. This value can be replaced for a specific relationship using the fetchType attribute on it.
2. aggressive LazyLoading: default = TRUE
When enabled, an object with lazy loaded properties will be fully loaded when you call any of the lazy properties. Otherwise, each property is loaded on demand.
Using the following attributes, I tried the following code:
a. JAVA Classes:
Feedback.java
public class Feedback implements Serializable { private static final long serialVersionUID = 1L; private int id; private String message; private User sender; private boolean seen;
User.java
public class User implements Serializable, { private static final long serialVersionUID = 1L; private int id; private String email;
b. DB schema:
Review table
Table "public.feedback" Column | Type | Modifiers -------------+-----------+------------------------------------------------------- id | integer | PRIMARY KEY seen | boolean | not null sender_id | integer | FOREIGN KEY (sender_id) REFERENCES users(id) message | text |
User table:
Table "public.users" Column | Type | Modifiers -------------+----------+---------------------------------------------------- id | integer | PRIMARY KEY email | text |
with. Configuring MyBatis via the JAVA API:
DataSource dataSource = new PGSimpleDataSource(); ((PGSimpleDataSource) dataSource).setServerName("localhost"); ((PGSimpleDataSource) dataSource).setDatabaseName(dbName); ((PGSimpleDataSource) dataSource).setPortNumber(5432); ((PGSimpleDataSource) dataSource).setUser(new UnixSystem().getUsername()); ((PGSimpleDataSource) dataSource).setPassword(""); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment(dbName, transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.addMapper(FeedbackMapper.class); // configuration.setAggressiveLazyLoading(false); sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
D. Querying DB and DB queries in the Feedbackmapper file:
d.1 Code in reverse order:
@Select("SELECT f.id, f.message, f.seen, f.sender_id FROM feedback f WHERE f.id= #{feedbackId}") @Results(value = { @Result(property = "id", column = "id"), @Result(property = "sender", column = "sender_id", javaType = User.class, one = @One(select = "getUser", fetchType=FetchType.DEFAULT)) }) public Feedback getFeedback(@Param("feedbackId") int feedbackId); @Select("SELECT id, email FROM users WHERE id=#{id}") public User getUser(int id);
d.2: Code for calling requests in feedbackMapper
// setup Mybatis session factory and config Feedback feedback =feedbackMapper.getFeedback(70000); System.out.println(feedback);
But still, the sender object is populated with a getFeedback (id) request. I expect that the sender object should not be filled immediately, but only when I call getSender () on the selected feedback object. Please help .
My recent observations:
The Mybatis team was really mistaken in its documentation, that is, in the documentation:
lazyLoadingEnabled: default = TRUE
aggressive LazyLoading: default = TRUE
But looking at their source code:
protected boolean lazyLoadingEnabled = false; protected boolean aggressiveLazyLoading = true;
** However, this is a fix, the results are not affected, and lazy loading does not work :( **