Java and its Related Technologies
Be in touch with this blog to learn more technical things on Java and its related Technologies.
Wednesday, March 3, 2010
Unix Useful commands (Advanced)
du -s * | sort -n | tail - To get the list of files and folders with more memory usage in ascending order.
Merging a Detached Object in Hibernate
What is detached Object in Hibernate: When an object has been persisted thru a session and when you close the session the Object in the memory will become detached. First portion of the example show this.
But hibernate does not allow to have two objects representing the same row, when you try to save the detached object hibernate throws an Exception.
Solution: Hibernate has a solution to reattach detached object using session.merge() method. Second portion of the below example show the same.
Dettachached Object merging in hibernate Example:
Public Class DetachDemo {
public static void main(String[] args) {
Session ses=new Configuration().configure().buildSessionFactory().openSession(); // Create Session.Transaction tx=ses.beginTransaction(); // Begin Transaction.
Student st=new Student();
st.setM1(78);
st.setM2(99);
st.setM3(88);
st.setName("Murali & Swathi");
ses.save(st); // Save the object st.
tx.commit();
ses.close();
// Now st becomes detached
Session ses1=new Configuration().configure().buildSessionFactory().openSession();
Transaction tx1=ses1.beginTransaction();
st.setName("Karim & Murali");
ses1.merge(st); // Merging the existing object with new session.
tx1.commit();
ses1.close();
}
}
But hibernate does not allow to have two objects representing the same row, when you try to save the detached object hibernate throws an Exception.
Solution: Hibernate has a solution to reattach detached object using session.merge() method. Second portion of the below example show the same.
Dettachached Object merging in hibernate Example:
Public Class DetachDemo {
public static void main(String[] args) {
Session ses=new Configuration().configure().buildSessionFactory().openSession(); // Create Session.Transaction tx=ses.beginTransaction(); // Begin Transaction.
Student st=new Student();
st.setM1(78);
st.setM2(99);
st.setM3(88);
st.setName("Murali & Swathi");
ses.save(st); // Save the object st.
tx.commit();
ses.close();
// Now st becomes detached
Session ses1=new Configuration().configure().buildSessionFactory().openSession();
Transaction tx1=ses1.beginTransaction();
st.setName("Karim & Murali");
ses1.merge(st); // Merging the existing object with new session.
tx1.commit();
ses1.close();
}
}
Labels:
detached objects,
example,
hibernate,
merging,
tutorial
Sunday, February 14, 2010
Unix VI Commands Summary and Help
General Startup
To use vi: vi filename
To exit vi and save changes: ZZ or :wq
To exit vi without saving changes: :q!
To enter vi command mode: [esc]
Counts
A number preceding any vi command tells vi to repeat that command that many times.
Cursor Movement
h move left (backspace)
j move down
k move up
l move right (spacebar)
[return] move to the beginning of the next line
$ last column on the current line
0 move cursor to the first column on the current line
^ move cursor to first nonblank column on the current line
w move to the beginning of the next word or punctuation mark
W move past the next space
b move to the beginning of the previous word or punctuation mark
B move to the beginning of the previous word,ignores punctuation
e end of next word or punctuation mark
E end of next word, ignoring punctuation
H move cursor to the top of the screen
M move cursor to the middle of the screen
L move cursor to the bottom of the screen
Screen Movement
G move to the last line in the file
xG move to line x
z+ move current line to top of screen
z move current line to the middle of screen
z- move current line to the bottom of screen
^F move forward one screen
^B move backward one line
^D move forward one half screen
^U move backward one half screen
^R redraw screen (does not work with VT100 type terminals)
^L redraw screen (does not work with Televideo terminals)
Inserting
r replace character under cursor with next character typed
R keep replacing character until [esc] is hit
i insert before cursor
a append after cursor
A append at end of line
O open line above cursor and enter append mode
Deleting
x delete character under cursor
dd delete line under cursor
dw delete word under cursor
db delete word before cursor
Copying Code
yy (yank)'copies' line which may then be put by the p(put) command. Precede with a count for multiple lines.
Put Command
brings back previous deletion or yank of lines, words, or characters
P bring back before cursor
p bring back after cursor
Find Commands
? finds a word going backwards
/ finds a word going forwards
f finds a character on the line under the cursor going forward
F finds a character on the line under the cursor going backwards
t find a character on the current line going forward and stop one character before it
T find a character on the current line going backward and stop one character before it
; repeat last f, F, t, T
Miscellaneous Commands
. repeat last command
u undoes last command issued
U undoes all commands on one line
xp deletes first character and inserts after
second (swap)
J join current line with the next line
^G display current line number
% if at one parenthesis, will jump to its mate
mx mark current line with character x
'x find line marked with character x
NOTE: Marks are internal and not written to the file.
Line Editor Mode
Any commands form the line editor ex can be issued
upon entering line mode.
To enter: type ':'
To exit: press[return] or [esc]
ex Commands
For a complete list consult the
UNIX Programmer's Manual
READING FILES
copies (reads) filename after cursor in file
currently editing
:r filename
WRITE FILE
:w saves the current file without quitting
MOVING
:# move to line #
:$ move to last line of file
SHELL ESCAPE
executes 'cmd' as a shell command.
:!'cmd'
To use vi: vi filename
To exit vi and save changes: ZZ or :wq
To exit vi without saving changes: :q!
To enter vi command mode: [esc]
Counts
A number preceding any vi command tells vi to repeat that command that many times.
Cursor Movement
h move left (backspace)
j move down
k move up
l move right (spacebar)
[return] move to the beginning of the next line
$ last column on the current line
0 move cursor to the first column on the current line
^ move cursor to first nonblank column on the current line
w move to the beginning of the next word or punctuation mark
W move past the next space
b move to the beginning of the previous word or punctuation mark
B move to the beginning of the previous word,ignores punctuation
e end of next word or punctuation mark
E end of next word, ignoring punctuation
H move cursor to the top of the screen
M move cursor to the middle of the screen
L move cursor to the bottom of the screen
Screen Movement
G move to the last line in the file
xG move to line x
z+ move current line to top of screen
z move current line to the middle of screen
z- move current line to the bottom of screen
^F move forward one screen
^B move backward one line
^D move forward one half screen
^U move backward one half screen
^R redraw screen (does not work with VT100 type terminals)
^L redraw screen (does not work with Televideo terminals)
Inserting
r replace character under cursor with next character typed
R keep replacing character until [esc] is hit
i insert before cursor
a append after cursor
A append at end of line
O open line above cursor and enter append mode
Deleting
x delete character under cursor
dd delete line under cursor
dw delete word under cursor
db delete word before cursor
Copying Code
yy (yank)'copies' line which may then be put by the p(put) command. Precede with a count for multiple lines.
Put Command
brings back previous deletion or yank of lines, words, or characters
P bring back before cursor
p bring back after cursor
Find Commands
? finds a word going backwards
/ finds a word going forwards
f finds a character on the line under the cursor going forward
F finds a character on the line under the cursor going backwards
t find a character on the current line going forward and stop one character before it
T find a character on the current line going backward and stop one character before it
; repeat last f, F, t, T
Miscellaneous Commands
. repeat last command
u undoes last command issued
U undoes all commands on one line
xp deletes first character and inserts after
second (swap)
J join current line with the next line
^G display current line number
% if at one parenthesis, will jump to its mate
mx mark current line with character x
'x find line marked with character x
NOTE: Marks are internal and not written to the file.
Line Editor Mode
Any commands form the line editor ex can be issued
upon entering line mode.
To enter: type ':'
To exit: press[return] or [esc]
ex Commands
For a complete list consult the
UNIX Programmer's Manual
READING FILES
copies (reads) filename after cursor in file
currently editing
:r filename
WRITE FILE
:w saves the current file without quitting
MOVING
:# move to line #
:$ move to last line of file
SHELL ESCAPE
executes 'cmd' as a shell command.
:!'cmd'
Subscribe to:
Posts (Atom)