I am trying to follow the same methods listed in this blog here to avoid the N+1 problem.
I have created 5 classes with a many to 1 relationship as follows:
Trunk -> Branch 1 -> Branch 2 -> Branch 3 ->Leaf
I am trying to build my XML mapper which complies but fails at runtime with an error that there is no property 'Branch2' in 'class com.mytest.branch1'
I have defined my XML mapper as
<mapper namespace="com.mytest.test">
<resultMap id="resultTest" type="com.mytest.test.trunk">
<id property="TrunkID" column="TRUNK_ID" />
<collection property="Branch1" column="BRANCH_1_ID" javaType="ArrayList" ofType="com.mytest.test.branch1">
<id property="branch1ID" column="BRANCH_1_ID">
<collection property="Branch2" column="BRANCH_2" javaType="ArrayList" ofType="com.mytest.test.branch2">
<id property="branch2ID" column="BRANCH_2_ID">
<collection property="Branch3" column="BRANCH_3" javaType="ArrayList" ofType="com.mytest.test.branch3">
<id property="branch3ID" column="BRANCH_3_ID">
<result property="Value" column="Value">
</collection>
</collection>
</collection>
</resultMap>
<select id="getAllNodes" resultMap="resultTest">
select n.TRUNK_ID,b.BRANCH_1_ID,b.BRANCH_2_ID,b.BRANCH_3_ID,b.VALUE FROM node n join branches b on n.node_id=b.node_id
</select>
</mapper>
Class Trunk:
public class Trunk {
private int TrunkID;
private List<Branch1> Branch1;
//getters and setters
}
Class Branch1:
public class Branch1{
private int Branch1ID;
private List<Branch2> Branch2;
//getters and setters ...
}
Class Branch2:
public class Branch2{
private int Branch2ID;
private List<Branch3> Branch3;
//getters and setters ...
}
Class Branch3:
public class Branch3{
private int Branch3ID;
//getters and setters ...
}
There are two things here.
First, did you even try to compile your code?
BranchX classes (and Trunk, too) don't have names for List fields.
It should look like this:
public class Trunk {
private int TrunkID;
private List<Branch1> listOfBranches; // name added
}
So please fix Trunk, Branch1 and Branch2 to follow the abouve example.
The second thing is that nested <collection>s don't work this way.
Please refer to the docs.
They say:
property: The field or property to map the column result to. If a matching JavaBeans property exists for the given name, then that will be used. Otherwise, MyBatis will look for a field of the given name.
So if you write
<collection property="Branch1" column="BRANCH_1_ID" javaType="ArrayList" ofType="com.mytest.test.branch1">
<id property="branch1ID" column="BRANCH_1_ID">
<collection property="Branch2" column="BRANCH_2" javaType="ArrayList" ofType="com.mytest.test.branch2">
...
</collection>
</collection>
the outer <collection> element is ArrayList of Branch1.
It has two inner elements:
<id> which is correctly mapped to branch1ID field in Branch1 class
<collection> which is supposed to be mapped to Branch2 field in Branch1 class - which is not existing.
Suggested fix for this is to name all List fields in your classes adequately, i.e:
public class Trunk {
private int TrunkID;
private List<Branch1> branch1;
}
public class Branch1{
private int Branch1ID;
private List<Branch2> branch2;
}
etc.
Hope that helps to understand what the problem was.
Related
I'm unable to delete a child record while I'm updating (not deleting) the parent record. Also, I've read other posts, but it seems most of the others are using annotations rather than xml, so it can be difficult to see how they relate to my issue.
I have two tables: The EventInfo table that holds information about events and then the EventLicenseType table that only has two columns and both of those columns make up the primary key; one of the columns in the EventLicenseType table is a foreign key to the EventInfo table.
The problem is I can't seem to delete an EventLicenseType record. I've tried a bunch of different things and nothing is working for me. It seems like hibernate wants to put null as the eventinfoId column, which of course doesn't work. I have tried clearing out the Set and then doing the merge, and also specifically calling session.delete(eventlicenseTypeRec) and then doing the merge. Neither is working for me.
EventInfo.hbm.xml file:
<hibernate-mapping default-lazy="true">
<class name="Eventinfo" table="PA_EVENTINFO">
<id name="eventInfoId" type="int"
column="PA_EVENTINFOID">
<generator class="native" />
</id>
<property name="eventTypeId" type="java.lang.String"
column="PA_EVENTTYPEID" length="255" />
...Other columns not shown here for brevity...
<set name="eventLicenceTypeIds" lazy="false" cascade="all-delete-orphan">
<key column="PA_EVENTINFOID"/>
<one-to-many class="EventLicenseType" />
</set>
</class>
EventLicenseType.hbm.xml file:
<hibernate-mapping default-lazy="true">
<class name="EventLicenseType" table="PA_EVENTLICENSETYPE">
<composite-id>
<key-property name="licenseTypeId" type="java.lang.Integer" column="PA_LICENSETYPE"/>
<key-property name="eventInfoId" type="java.lang.Integer" column="PA_EVENTINFOID"/>
</composite-id>
</class>
Here is the EventInfo class. Again, there are more fields in the actual file, this is just the important pieces:
public class Eventinfo implements Serializable {
/** identifier field */
private int eventInfoId;
/** nullable persistent field */
#Field(name="eventInfo_eventTypeId")
private String eventTypeId;
#IndexedEmbedded
private Set<EventLicenseType> eventLicenceTypeIds;
/** default constructor */
public Eventinfo() {}
public int getEventInfoId() {
return this.eventInfoId;
}
public void setEventInfoId(int eventInfoId) {
this.eventInfoId = eventInfoId;
}
public String getEventTypeId() {
return this.eventTypeId;
}
public void setEventTypeId(String eventTypeId) {
this.eventTypeId = eventTypeId;
}
public Set<EventLicenseType> getEventLicenceTypeIds() {
return eventLicenceTypeIds;
}
public void setEventLicenceTypeIds(Set<EventLicenseType> eventLicenceTypeIds) {
this.eventLicenceTypeIds = eventLicenceTypeIds;
}
Here is the EventLicenseType class
public class EventLicenseType implements Serializable{
#Field
private int licenseTypeId;
private int eventInfoId;
public int getLicenseTypeId() {
return licenseTypeId;
}
public void setLicenseTypeId(int licenseTypeId) {
this.licenseTypeId = licenseTypeId;
}
public int getEventInfoId() {
return eventInfoId;
}
public void setEventInfoId(int eventInfoId) {
this.eventInfoId = eventInfoId;
}
}
Here is the method I'm executing in my DAO. For now there is only one record associated to the eventInfo record, so I'm just trying to see if I can delete that one. (Also note that eventinfo is defined in the method that surrounds this one).
public Eventinfo execute(Session session) throws Exception {
//Get the existing eventInfo record
Eventinfo existing = (Eventinfo)session.get(Eventinfo.class, eventinfo.getEventInfoId());
Iterator iter = existing.getEventLicenceTypeIds().iterator();
if (iter.hasNext()) {
EventLicenseType license = (EventLicenseType) iter.next();
iter.remove();
session.delete(license);
}
session.flush();
return (Eventinfo) session.merge(eventinfo);
}
On the above session.flush() line, I get an error: java.sql.BatchUpdateException: Cannot insert the value NULL into column 'PA_EVENTINFOID', table 'PA_EVENTLICENSETYPE'; column does not allow nulls. UPDATE fails. It shows that hibernate is trying to do:
update PA_EVENTLICENSETYPE set PA_EVENTINFOID=null where PA_EVENTINFOID=?
Why can't it just delete the record? Why is it trying to do an update??
I also tried changing the code to the below and get the same error.
public Eventinfo execute(Session session) throws Exception {
//Clear out the list
eventinfo.getEventLicenceTypeIds().clear();
return (Eventinfo) session.merge(eventinfo);
}
Can anyone help me with what I'm missing, or point me in the right direction?
You need to see whether the mapping between two tables are unilateral or bilateral. Basically you need to think or the rows of two tables as objects and cut all the ties between the objects of EventInfo and EventLicenceType tables. So if the relation is only from EventInfo -> EventLicenseType, you need to set the value of the EventLicenseType set to null in EventInfo object. Also if there is a mapping from EventLicenseType, you need to set the value of joining column to null. Then merge() the EventInfo object.
Noneed to explicitly delete or remove the EventLicenseType object. If here are no references to EventLicenseType object, the JVM will collect it as garbage.
Hope that solves your problem.
I need some help here, I am trying to return a list of objects, inside a parent object using myBatis.
THE PROBLEM :
Before you begin reading the code below - My error that I get back is :
SqlSession operation; nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 4102
What is interesting here is that this somehow means I am hitting the stored procedure, seeing the amount of data there is, and erroring due to having too many results because myBatis thinks I am using selectOne() - Which is not true? I will add, that 4102 is the exact number of records in the table I am trying to pull this data from.
MY CODE :
Here are the result maps for the parent and child objects :
<resultMap id="ParentObjectMap" type="com.company.product.mybatis.model.ParentObject">
<collection property="children" resultMap="childrenMap"/>
</resultMap>
<resultMap id="childrenMap" type="com.company.product.mybatis.model.ChildObject">
<id column="ChildId" jdbcType="BIGINT" property="childId" />
<result column="Name" jdbcType="VARCHAR" property="name" />
</resultMap>
Here is the code for each of the above maps.
public class ParentObject implements Serializable {
private long id;
private List<ChildObject> childrenMap;
/* GETTERS AND SETTERS EXCLUDED FOR BREVITY. */
}
And the class for the Child object :
public class ChildObject implements Serializable {
private long childId;
private String name;
/* GETTERS AND SETTERS REMOVED FOR BREVITY. */
}
Here Is the Stored Procedure which I am calling, which aims to return the data :
ALTER PROCEDURE dbo.PR_Children_Get
AS
SET NOCOUNT ON
BEGIN
SELECT
tp.ChildId,
tp.Name
FROM dbo.Children tp WITH (NOLOCK)
END
GO
And here is how I am executing that procedure in my mapper :
<select id="getChildren" resultMap="ParentObjectMap">
exec [dbo].[PR_Children_Get]
</select>
Here is the interface through which I access my mapper :
#Override
public ParentObject getChildren() throws Exception {
ParentObject result = ParentObjectMapper.getChildren();
return result;
}
Here is the interface of that ParentObjectMapper :
/* Hiding imports for brevity */
public interface ParentObjectMapper {
// Get the list children, the list should be a property within the parent object.
ParentObject getChildren();
}
I suppose you execute through a mapper interface and the method would be: ParentObject getChildren(); this explicitly expect a single result because this is not a collection type, then the error you got.
But I'm not telling you should change to List<ParentObject> getChildren(); to avoid this error ... or maybe you should ... for a while, because it would help understanding the issue:
With your code Mybatis creates a new ParentObject for each result row because the parentId is never returned and Mybatis does not assume you have a single parent. Then the query shall return a parentId column and the ParentObjectMap start with <id column="parentId" property="id"/>, so that Mybatis knows how to group children by their parent.
I have the following stored procedure - simply returning two integers for now :
USE [DB_NAME_HERE]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[PR_PROCEDURE_NAME]
SELECT 0 AS ValueA,0 AS ValueB
and I have the following mapper XML :
<resultMap id="BaseResultMap" type="com.companyname.service.mybatis.model.modelClass">
<result column="ValueA" jdbcType="INTEGER" property="valueA" />
<result column="ValueB" jdbcType="INTEGER" property="valueB" />
</resultMap>
<select id="getPurgedTokens" resultMap="BaseResultMap" statementType="CALLABLE">
{call PR_PROCEDURE_NAME(
#{resultContainer.valueA, mode=OUT, jdbcType=INTEGER},
#{resultContainer.valueB, mode=OUT, jdbcType=INTEGER}
)}
</select>
Then I have the following java class for the mapping :
public class modelClass implements Serializable {
private int valueA;
private int valueB;
public int getValueA() { return this.valueA; }
public void setValueA(int valueA) { this.valueA = valueA; }
public int getValueB() { return this.valueB; }
public void setValueB(int valueB) { this.valueB= valueB; }
}
But when I run up my application, and attempt to get any kind of data, I get the following error :
code=DataIntegrityViolationException,messages=[SqlSession operation; SQL []; Invalid JDBC call escape at line position 10.; nested exception is java.sql.SQLException: Invalid JDBC call escape at line position 10., Invalid JDBC call escape at line position 10.]]
All help here is greatly appreciated - I have been trying to get this working for days!
EDIT : UPDATE WITH EXACT COPIES OF LATEST CODE!
MapperXML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.company.product.mybatis.mappers.MapperXML">
<!-- Commented out the base result map because now we link directly in the paramter type? -->
<!--
<resultMap id="BaseResultMap" type="com.companyname.service.mybatis.model.modelClass">
<result column="ValueA" javaType="java.lang.Integer" jdbcType="INTEGER" property="valueA"/>
<result column="ValueB" javaType="java.lang.Integer" jdbcType="INTEGER" property="valueB"/>
</resultMap>-->
<!-- -------------------------------------------- -->
<select id="getPurgedTokens" parameterType="com.companyname.service.mybatis.model.ModelClass" statementType="CALLABLE">
{call PR_TokenPurge_Clean(
#{dvalueA, mode=OUT, jdbcType=INTEGER},
#{valueB, mode=OUT, jdbcType=INTEGER}
)}
</select>
</mapper>
ModelClass
package com.companyname.service.mybatis.model;
import java.io.Serializable;
public class ModelClass implements Serializable {
private int valueA;
private int valueB;
public int getVavlueA() { return this.valueA; }
public void setValueA(int valueA) { this.valueA = valueA; }
public int getValueB() { return this.valueB; }
public void setValueB(int valueB) { this.valueB = valueB; }
}
DAO
package com.company.product.dao.mybatis;
[imports (removed for brevity and security)...]
#Repository
public class ModelClassDaoMybatis implements ModelClassDao {
private TokenPurgeModelMapper tokenPurgeModelMapper;
private SqlSessionOperations sqlSessionOperations;
#Override
public TokenPurgeModel purgeTokens()
{
if (this.sqlSessionOperations.selectOne(".getPurgedTokens") != null)
{
System.out.println("I FOUND A VALUE!!!!!!");
return (TokenPurgeModel) sqlSessionOperations.selectOne(testing);
}
else
{
System.out.println("No value found for select statement");
return null;
}
}
#Override
public List<TokenPurgeModel> getPurgedTokens() {
return tokenPurgeModelMapper.getPurgedTokens();
}
#Required
public void setTokenPurgeModelMapper(final TokenPurgeModelMapper tokenPurgeMapper) {
this.tokenPurgeModelMapper = tokenPurgeMapper;
}
#Required
public void setSqlSessionOperations(SqlSessionOperations sqlSessionOperations) {
this.sqlSessionOperations = sqlSessionOperations;
}
}
MODEL MAPPER CLASS
package com.company.product.mybatis.mappers;
[imports (removed for brevity and security)...]
public interface TokenPurgeModelMapper {
// This is the one I am using right now. (as seen in the DAO!)
TokenPurgeModel purgeTokens();
// Get the list of tokens which have been removed from the PAN table.
List<TokenPurgeModel> getPurgedTokens(); // This is actually never used - will be used in later funcationality!
}
THANKS for looking at this for me!
Indeed, first thing is to use syntax {call PR_PROCEDURE_NAME()}
Then it depends on how the procedure is written and on which SGBD is used.
There are multiple possibilities including:
Return the values as procedure OUT parameters:
<select id="getPurgedTokens" parameterType="com.companyname.service.mybatis.model.modelClass" statementType="CALLABLE">
{call PR_PROCEDURE_NAME(
#{valueA, mode=OUT, jdbcType=INTEGER},
#{valueB, mode=OUT, jdbcType=INTEGER}
)}
</select>
The modelClass parameter does not actually provide input parameters in this case and is used here just as target container for OUT parameters; and then you don't need resultMap.
If the result becomes more complex, return a cursor on the select:
{call PR_PROCEDURE_NAME(
#{resultContainer.resultList, mode=OUT, jdbcType=CURSOR, javaType=java.sql.ResultSet, resultMap=BaseResultMap}
)}
Or a function (instead of a procedure) returning the cursor fetching the Select (some SGBD may allow writing just the SELECT in the body), then merely:
SELECT function_name from DUAL
bound with the resultMap
I couldn't able to find how to achieve lazy loading(even in MyBatis docs).
My mapper xml is shown below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.FooMyBatisLazyFetch">
<select id="callFooProc"
parameterType="com.example.FooProcBundle"
statementType="CALLABLE">
{call FooProc(
#{arg1, jdbcType=VARCHAR, mode=IN},
#{arg2, jdbcType=VARCHAR, mode=IN},
#{arg3, jdbcType=VARCHAR, mode=IN},
#{error, jdbcType=NUMERIC, mode=OUT},
#{res2, jdbcType=CURSOR, mode=OUT, resultMap=FooProcResult}
)
}
</select>
<resultMap id="FooProcResult" type="com.example.FooProcResult">
<result property="bar1" column="barcol1"/>
<result property="bar2" column="barcol2"/>
<result property="bar3" column="barcol3"/>
<result property="bar4" column="barcol4"/>
<result property="bar5" column="barcol5"/>
</resultMap>
</mapper>
Pojo Class:
public class FooProcResult {
private String bar1;
private String bar2;
private String bar3;
private String bar4;
private String bar5;
}
public class FooProcBoondle {
private String arg1;
private String arg2;
private String arg3;
private Integer error;
private List<FooProcResult> res2;
//getters,setters, etc
}
And usage code;
FooProcBundle bundle = new FooProcBundle();
bundle.setArg1("foo");
bundle.setArg2("bar");
bundle.setArg3("baz");
fooMyBatisLazyFetch.callFooProc(bundle);
Integer error = bundle.getError();
if(error == 123) /*some condition*/ {
List<FooProcResult> res2 = bundle.getRes2();
// iterate res2
--->// Only here CURSOR may be opened and executed
}
i.e. I don't want to fetch res2 unless my code explicitly request for it. That particular cursor is quite heavy, and I don't want to execute it when it's not required(but mybatis does it).
Also I want to apply this to generator-like procedures (Oracle call them "Pipelined Table Functions" they yield result, sleep and wait until caller fetches next row - wakeup and calculate next. Usually they called this way: SELECT * FROM TABLE(GenProc(arg1,arg2)).
Any ideas about the configuration required to achieve this?
Procedure output cursor parameters are processed in class org.apache.ibatis.executor.resultset.DefaultResultSetHandler
in method handleOutputParameters and then in method handleRefCursorOutputParameter. You will note that the code in its current state, does not allow to do want you seek, the only "custom option" used is the resultMap that must be provided. I also would have appreciated some more options just like lazy loading, custom result handler, and some logs to be able to monitor the actual execution time and the fetch time.
This could be achieved in JDBC and would require configuration that is not implemented in the framework.
I have a class called WebAsset:
public class WebAsset {
private Long id;
private String url;
private int status;
//more fields that are not relevent
}
I need to be able to show relationships between WebAsset, so I created a table for the relationship and a composite key class.
public class WebAssetReferencePK {
private Long sourceAssetId;
private Long targetAssetId;
}
public class WebAssetReference {
private WebAssetReferencePK wpk;
private Long updateTime;
}
We are forced to use an older version of Hibernate so we need to use xml files instead of annotaions. Here is the mapping for the reference class:
<class name="ca.gc.cra.www.crawler.valueobject.WebAssetReference" table="webassetreference">
<composite-id name="webAssetReferencePK" class="ca.gc.cra.www.crawler.valueobject.WebAssetReferencePK">
<key-property name="sourceAsset" type="java.lang.Long" column="sourceAssetId" />
<key-property name="targetAsset" type="java.lang.Long" column="targetAssetId" />
</composite-id>
<property name="updateTime" type="java.lang.Long" column="updatetime" not-null="true" />
</class>
In the composite key I get what I expect in the database with 2 ids related to each other. But when I try to query with HQL or Criteria it doesn't work since there is no direct relation between the PK class and WebAsset and I need to be able to do a join between WebAsset and WebAssetReference. If I try to change the composite key types from java.lang.Long to WebAsset then hibernate stores the whole object in the WebAssetReference table instead of just the ids.
An example of what I am trying to do is if I have a sourceAssetId I want to return all the targetAssetIds with the same source, but I don't want the ids themselves I want the WebAsset that is the primary key for each targetAssetId.
I have been searching around for the answer but every example I can find are just simple examples that don't relate.
Update 1: With continued searching I finally found the answer. Instead of key-property I need to use key-many-to-one. I haven't tried a join yet but everything else looks right so this should be the answer.
Update 2: Can't get the query to work with HQL. Here is th SQL of what I am trying to do:
select * from webasset as wa join webassetreference as war on war.targetassetid=wa.webasset_id where war.sourceassetid=?
Here is the HQL that is not working:
FROM WebAsset JOIN WebAssetReference WebAssetReference.WebAssetReferencePK.targetAsset=WebAsset WHERE WebAssetReference.WebAssetReferencePK.sourceAsset = :sourceAsset
I get the following error with HQL: ERROR - line 1:89: unexpected token: .
I'll keep trying but I can't seem to figure out the HQL.
I discovered how to do this. In the case I have above it will not work since I have 2 columns joining to the same table. However if I use the same WebAsset class above and instead use this class:
public class TreeNode implements Comparable<TreeNode>{
private String nodeUrl;
private Long id;
private Boolean folder;
private transient WebAsset nodeAsset = null;
}
With this .hbm.xml file:
<class name="ca.gc.cra.www.crawler.valueobject.TreeNode" table="TreeNode">
<id name="id" type="java.lang.Long" column="treenode_id" >
<generator class="identity"/>
</id>
<many-to-one name="nodeAsset" class="ca.gc.cra.www.crawler.valueobject.WebAsset" column="nodeAsset_id" lazy="false" not-null="false" cascade="none" unique="true" />
<property name="folder" type="java.lang.Boolean" column="folder" not-null="true" />
<property name="nodeUrl" length="512" type="java.lang.String" column="nodeUrl" not-null="true" />
<set name="children" table="TreeNode" inverse="false" lazy="true" >
<key column="parentnode_id"/>
<one-to-many class="ca.gc.cra.www.crawler.valueobject.TreeNode" />
</set>
</class>
You can then use this code to retrieve the join:
Session session = HibernateUtil.getSession();
try {
String hql = "FROM TreeNode tn JOIN tn.nodeAsset WHERE tn.id=5";
Query query = session.createQuery(hql);
List result = query.list();
System.out.println("done");
} catch (HibernateException e) {
e.printStackTrace();
throw new Exception("Query failed", e);
} finally {
session.flush();
session.close();
}
Hibernate can then perform the join correctly. The result will be a List containing an Object array for each entry. The Object contains the 2 classes that are part of the join. You have to cast the Object with (Object[]) to access the elements and then cast each on to the appropriate class.
I would recommend against this approach because Hibernate will attempt to load all connected classes as well. With the example above I was getting 1 row from TreeNode yet it generated 19 select statements. I even attempted to set the connected classes to lazy load and it still generated all the selects.