May 4, 2009
Eclipse Code Style Sample (You can import!)
MERGE INTO Statement
Let's look at merge statement and stduy.
MERGE INTO table_name1 USING table_name2 ON (condition)
WHEN MATCHED THEN
UPDATE SET column1 = value1 [, column2 = value2 ...]
WHEN NOT MATCHED THEN
INSERT (column1 [, column2 ...]) VALUES (value1 [, value2 ...])
If a record exist, you want to update it, and if the record does not exist, you want to insert the record. To do this, merge statement is introduced.
In oracle show examples. (http://www.oracle.com/technology/products/oracle9i/daily/Aug24.html)
Without Merge statement, it will be shown belows. It is used to use in Oracle 8.
UPDATE
(SELECT
S.TIME_ID ,S.STORE_ID ,S.REGION_ID,
S.PARTS s_parts ,S.SALES_AMT s_sales_amt ,S.TAX_AMT s_tax_amt ,S.DISCOUNT s_discount,
D.PARTS d_parts ,D.SALES_AMT d_sales_amt ,D.TAX_AMT d_tax_amt ,D.DISCOUNT d_discount
FROM SALES_JUL01 S, SALES_FACT D
WHERE D.TIME_ID = S.TIME_ID
AND D.STORE_ID = S.STORE_ID
AND D.REGION_ID = S.REGION_ID) JV
SET d_parts = d_parts + s_parts,
d_sales_amt = d_sales_amt + s_sales_amt,
d_tax_amt = d_tax_amt + s_tax_amt,
d_discount = d_discount + s_discount
;
INSERT INTO SALES_FACT (
TIME_ID,STORE_ID ,REGION_ID,
PARTS ,SALES_AMT ,TAX_AMT ,DISCOUNT)
SELECT
S.TIME_ID ,S.STORE_ID ,S.REGION_ID,
S.PARTS ,S.SALES_AMT ,S.TAX_AMT ,S.DISCOUNT
FROM SALES_JUL01 S
WHERE (S.TIME_ID, S.STORE_ID, S.REGION_ID) NOT IN (
SELECT D.TIME_ID, D.STORE_ID, D.REGION_ID
FROM SALES_FACT D
)
;
It is very long sentences and verbosely.
So, in Oracle 9 introduce Merge statement. Upper statements changed to belows in ussing merge.
USING SALES_JUL01 S
ON (D.TIME_ID = S.TIME_ID
AND D.STORE_ID = S.STORE_ID
AND D.REGION_ID = S.REGION_ID)
WHEN MATCHED THEN
UPDATE
SET d_parts = d_parts + s_parts,
d_sales_amt = d_sales_amt + s_sales_amt,
d_tax_amt = d_tax_amt + s_tax_amt,
d_discount = d_discount + s_discount
WHEN NOT MATCHED THEN
INSERT (D.TIME_ID ,D.STORE_ID ,D.REGION_ID,
D.PARTS ,D.SALES_AMT ,D.TAX_AMT ,D.DISCOUNT)
VALUES (
S.TIME_ID ,S.STORE_ID ,S.REGION_ID,
S.PARTS ,S.SALES_AMT ,S.TAX_AMT ,S.DISCOUNT);
It is very simple, easy to undestand.
There is advancements and enhancements on the merge statement in Oracle 10g.
1) Optional insert and update
-- No matched clause, insert only.
MERGE INTO test1 a
USING all_objects b
ON (a.object_id = b.object_id)
WHEN NOT MATCHED THEN
INSERT (object_id, status)
VALUES (b.object_id, b.status);
-- No not-matched clause, update only.
MERGE INTO test1 a
USING all_objects b
ON (a.object_id = b.object_id)
WHEN MATCHED THEN
UPDATE SET a.status = b.status;
2) Conditional clause
After to update or delete statement after WHEN MATCHED THEN or WHEN NOT MATCHED THEN clause can have where clauses.
With Oracle 10g, we can can now apply additional conditions(WHERE) to the UPDATE or INSERT operation within a MERGE. It is extremely useful if we have different rules for when a record is updated or inserted but we do not wish to restrict the ON condition that joins source and target together.
MERGE INTO test1 a
USING all_objects b
ON (a.object_id = b.object_id)
WHEN MATCHED THEN
UPDATE SET a.status = b.status
WHERE b.status != 'VALID'
WHEN NOT MATCHED THEN
INSERT (object_id, status)
VALUES (b.object_id, b.status)
WHERE b.status != 'VALID';
-- No matched clause, insert only.
MERGE INTO test1 a
USING all_objects b
ON (a.object_id = b.object_id)
WHEN NOT MATCHED THEN
INSERT (object_id, status)
VALUES (b.object_id, b.status)
WHERE b.status != 'VALID';
-- No not-matched clause, update only.
MERGE INTO test1 a
USING all_objects b
ON (a.object_id = b.object_id)
WHEN MATCHED THEN
UPDATE SET a.status = b.status
WHERE b.status != 'VALID';
3) Deleting during merging
You can delete conditionally DELETE rows during an UPDATE operation.
MERGE INTO test1 a
USING all_objects b
ON (a.object_id = b.object_id)
WHEN MATCHED THEN
UPDATE SET a.status = b.status
WHERE b.status != 'VALID'
DELETE WHERE (b.status = 'VALID');
* examples are referenced on http://www.oracle-base.com/articles/10g/MergeEnhancements10g.php
Impossible to get merged row count in ibatis. (to 2.3.4)
But, contrary to your expectation, the is no return value. Because SqlClient use internally SqlExecutor class(com.ibatis.sqlmap.engine.execution.SqlExecutor), a executeUpdate method of the SqlExecutor return update count of prepareSatement by calling ps.getUpdateCount(), but the that executeUpdate method alway return 0.
To current(ibatis 2.3.4), if you use the update in ibatis used internally in used merge into statment, there is no measures to confirm the return value.
Clone method in Java
I want to clone a ArrayList object. As given java api, cloning is available.
ArrayList
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}
Clone method is protected method of class. so, if you want clone a object, you have to call clone of The Object class in interal class, not external. It means you can not call the clone method directly from the outside.
So. If you have your own class and want to clone, you have to use belows.
public class NotiConnection {
public Object clone(){
return super.clone();
}
// or
public Object cloning() {
return clone();
}
}
The described In clone method in java api.
clone
protected Object clone() throws CloneNotSupportedException
- Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:
will be true, and that the expression:x.clone() != x
will be true, but these are not absolute requirements. While it is typically the case that:x.clone().getClass() == x.getClass()
will be true, this is not an absolute requirement.x.clone().equals(x)
By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case thatx.clone().getClass() == x.getClass().
By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.
The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then aCloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.
The class Object does not itself implement the interface Cloneable, so calling the clonemethod on an object whose class is Object will result in throwing an exception at run time.
- Returns:
- a clone of this instance.
- Throws:
CloneNotSupportedException- if the object's class does not support theCloneableinterface. Subclasses that override theclonemethod can also throw this exception to indicate that an instance cannot be cloned.- See Also:
If you implement Cloneable , you can call directly that method by implements "publicObject clone() throws CloneNotSupportedException ".
Cloneable interface is just a interface that have no implement any methods. It means notify the own class have the clone method to the developers.
Reference site : http://www.java-tips.org/java-se-tips/java.lang/how-to-implement-cloneable-interface.html
public class CloneExp implements Cloneable {
private String name;
private String address;
private int age;
private Department depart;
public CloneExp(){
}
public CloneExp(String aName, int aAge, Department aDepart) {
this.name = aName;
this.age = aAge;
this.depart = aDepart;
}
public Object clone() throws CloneNotSupportedException {
CloneExp clone=(CloneExp)super.clone();
// make the shallow copy of the object of type Department
clone.depart=(Department)depart.clone();
return clone;
}
public static void main(String[] args) {
CloneExp ce=new CloneExp();
try {
// make deep copy of the object of type CloneExp
CloneExp cloned=(CloneExp)ce.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}