More Examples/Snippets
Saturday, June 13, 2009
ManyToOne mapping in case of primary key composed of foreign key
For a primary key composed of a foreign key from a ManyToOne in JPA 1.0 you need to map the foreign key field twice. With a basic @Id mapping and with the @ManyToOne, the @ManyToOne should be set read-only (insertable, updatable=false, or use @PrimaryKeyJoinColumn)
Java Rest Framework Links
Links
http://objectif-naiade.blogspot.com/2007/11/java-rest-framework-jersey.html
http://www.oreillynet.com/onjava/blog/2007/04/restlet_lightweight_rest_frame.html
https://restlet.dev.java.net/
http://www.javaworld.com/javaworld/jw-10-2007/jw-10-resteasy.html
http://twit88.com/blog/2008/09/24/java-open-source-rest-framework/
http://www.restlet.org/about/features
https://cetia4.dev.java.net/
http://marxsoftware.blogspot.com/2009/01/easy-java-based-rest-testing-tools.html
http://www.javaworld.com/community/node/2193
http://objectif-naiade.blogspot.com/2007/11/java-rest-framework-jersey.html
http://www.oreillynet.com/onjava/blog/2007/04/restlet_lightweight_rest_frame.html
https://restlet.dev.java.net/
http://www.javaworld.com/javaworld/jw-10-2007/jw-10-resteasy.html
http://twit88.com/blog/2008/09/24/java-open-source-rest-framework/
http://www.restlet.org/about/features
https://cetia4.dev.java.net/
http://marxsoftware.blogspot.com/2009/01/easy-java-based-rest-testing-tools.html
http://www.javaworld.com/community/node/2193
JSF Links
http://jsfcentral.com/
http://jsftutorials.net/index.php
tutorials
http://www.exadel.com/tutorial/jsf/jsftutorial-kickstart.html
http://jsftutorials.net/jsf-development-guide/examples-and-tutorials.html
other sites
http://theopensourcery.com/javajsf.htm
http://www.roseindia.net/jsf/
Frameworks/Components
http://primefaces.prime.com.tr/en/
http://www.icefaces.org/
http://www.jboss.org/jbossrichfaces/
http://myfaces.apache.org/
tips
http://java.dzone.com/articles/making-distinctions-between
http://jsftutorials.net/index.php
tutorials
http://www.exadel.com/tutorial/jsf/jsftutorial-kickstart.html
http://jsftutorials.net/jsf-development-guide/examples-and-tutorials.html
other sites
http://theopensourcery.com/javajsf.htm
http://www.roseindia.net/jsf/
Frameworks/Components
http://primefaces.prime.com.tr/en/
http://www.icefaces.org/
http://www.jboss.org/jbossrichfaces/
http://myfaces.apache.org/
tips
http://java.dzone.com/articles/making-distinctions-between
JPA Hibernate Tips
using the old classes12 driver lib results in an issue where OracleDriver.getMajorVersion returns 0, which confuses hibernate.
Upgrading to ojdbc14.jar will fix the issue.
Design patterns
Safe Getters and Setters
-domain objects returning collection objects in getter should check for null
and return EMPTY_COLLECTION instead of null collection
-domain objects returning string objects in getter should check for null and
return empty string instead of null
Assert methods to validate method arguments
-call assert methods to check for null arguments instead of checking objects for null and throwing an IllegalArgumentException
-in graph of objects add getter methods to link child objects to parent objects
Null object Design pattern
-to avoid checking for null across the whole application. centralize the null checks within domain object, service object getters
elements2Attributes.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<xsl:output indent="yes"/>
<xsl:template match="*">
<xsl:copy>
<xsl:for-each select="@*|*[not(* or @*)]">
<xsl:attribute name="{concat(name(.), position())}">
<xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="*[* or @*]|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<xsl:output indent="yes"/>
<xsl:template match="*">
<xsl:copy>
<xsl:for-each select="@*|*[not(* or @*)]">
<xsl:attribute name="{concat(name(.), position())}">
<xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="*[* or @*]|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
attributes2Elements.xslt
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:copy>
<xsl:if test="@*">
<xsl:for-each select="@*">
<xsl:element name="{substring(name(), 1, string-length(name()) - 1)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:copy>
<xsl:if test="@*">
<xsl:for-each select="@*">
<xsl:element name="{substring(name(), 1, string-length(name()) - 1)}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
echo.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" normalization-form="fully-normalized"/>
<xsl:template match="/">
<xsl:apply-templates select="child::node()"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" normalization-form="fully-normalized"/>
<xsl:template match="/">
<xsl:apply-templates select="child::node()"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
unix bash commands
PS1='\h:\w>'
alias cmd="cat $homedir/.bashrc"
alias log2="tail -100f $file"
alias cdir="cd $dir"
alias u="cd /bea/wl815/user_projects/domains/$domain/admin/upload"
alias cls="clear"
alias c="clear"
alias dir="ls"
alias lsr="ls -ltr"
alias lsm="ls -ltR more"
alias lsa="ls -a "
alias chm="chmod -R 777 * "
alias log="cd log4j && pwd"
displays machine info by ip/name
nslookup
CLASSPATH=jkjkl; export;
echo $CLASSPATH
alias cmd="cat $homedir/.bashrc"
alias log2="tail -100f $file"
alias cdir="cd $dir"
alias u="cd /bea/wl815/user_projects/domains/$domain/admin/upload"
alias cls="clear"
alias c="clear"
alias dir="ls"
alias lsr="ls -ltr"
alias lsm="ls -ltR more"
alias lsa="ls -a "
alias chm="chmod -R 777 * "
alias log="cd log4j && pwd"
displays machine info by ip/name
nslookup
CLASSPATH=jkjkl; export;
echo $CLASSPATH
unix commands
sort filenames in unix in long format, ignore case
ls -l | sort -f -k 9
change to directory a
cda
alias cda="cd a"
tail log
loga
alias loga="tail -100f a"
execute unix commands on windows
oracle
case insensitive search
to retrieve records, search records case insensitive use upper function
to retrieve records, search records case insensitive use upper function
select * from User
where upper(name) like 'JOHN%'
select current date/execute function
select sysdate
from dual;
executing a function
select func(x)
from dual;
select number of records in table
select count(*)
from table
executing a procedure which returns a reference cursor
DECLARE
TYPE CurTyp IS REF CURSOR;
cur CurTyp;
BEGIN
DBMS_OUTPUT.ENABLE(1000000); -- enable output
cur := schema.pkg.proc_name(input);
END;
executing a procedure which returns a number
DECLARE
x number;
BEGIN
DBMS_OUTPUT.ENABLE(1000000);
x := schema.pkg.proc_name(input);
END;
executing a procedure which returns a varchar
DECLARE
str varchar(3000);
BEGIN
DBMS_OUTPUT.ENABLE(1000000);
str := schema.pkg.proc_name(input);
END;
printing separator
declare
str varchar(3000);
begin
dbms_output.put_line('------------------');
str := 'Hello';
dbms_output.put_line(str);
end;
string literal is enclosed in single quote
example 'hello'
assignment operator
:=
equality operator
=
not equal to operator
<>
line separator
;
For loop
DECLARE
ii number;
BEGIN
ii := 4;
FOR ii IN -2..20 LOOP
dbms_output.put_line(ii);
END LOOP;
END;
--fetching data from cursors to record
declare
type curtype is ref cursor;
cur curtyp;
type rectype is record
(
col1 number,
col2 varchar(30)
);
rec rectype;
begin
cur := proc(); --execute proc
loop
fetch cur into rec
exit when cur%notfound;
dbms_output.put_line(rec.col1 || ' ' || rec.col2);
end loop
close cur;
end;
jedit find replace regular expression
Find and replace new lines
Find: \n
Replace:
Find start of line
Find: ^
Find one of the following word: test1test2test3
Remember the matches: (test1test2test3)
Replace with the match: $1
Beanshell snippet: _1.equals("value1") ? "val2" : "val3"
Find: \n
Replace:
Find start of line
Find: ^
Find one of the following word: test1test2test3
Remember the matches: (test1test2test3)
Replace with the match: $1
Beanshell snippet: _1.equals("value1") ? "val2" : "val3"
ToTitleCase.bsh
public String titleCase(String text) {
String[] strs = text.split("_");
StringBuffer st = new StringBuffer();
for(int ii = 0; ii < line =" textArea.getCaretLine();" text =" textArea.getLineText(line);">
String[] strs = text.split("_");
StringBuffer st = new StringBuffer();
for(int ii = 0; ii < line =" textArea.getCaretLine();" text =" textArea.getLineText(line);">
swing-awt-frame
Frame
maximize
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Bring to Front
frame.setAlwaysOnTop(true);
frame.setAlwaysOnTop(false);
Request focus
frame.requestFocus();
Sizing,Visibility
frame.pack();
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Get Screen size
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
maximize
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Bring to Front
frame.setAlwaysOnTop(true);
frame.setAlwaysOnTop(false);
Request focus
frame.requestFocus();
Sizing,Visibility
frame.pack();
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Get Screen size
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
java tips
Format number to two digits
System.out.println(new DecimalFormat("00").format(8));
System.out.println(new DecimalFormat("00").format(18));
Invoke just before the appliction shutsdown
static {
//add runtime shutdown hook in a runnable
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
//...
}
});
}
System.out.println(new DecimalFormat("00").format(8));
System.out.println(new DecimalFormat("00").format(18));
Invoke just before the appliction shutsdown
static {
//add runtime shutdown hook in a runnable
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
//...
}
});
}
struts tiles reload
<action path="/tiles-admin/reload" type="org.apache.struts.tiles.actions.ReloadDefinitionsAction"> </action> <action path="/tiles-admin/view" type="org.apache.struts.tiles.actions.ViewDefinitionsAction"> </action>
weblogic jndi lookup
domain > servers > server
Listen Port: XXX
weblogic.jndi.Environment environment = new weblogic.jndi.Environment();
environment.setInitialContextFactory(weblogic.jndi.Environment.DEFAULT_INITIAL_CONTEXT_FACTORY);
environment.setProviderUrl("t3://domain:XXX");
environment.setSecurityPrincipal("user");
environment.setSecurityCredentials("pwd");
Context initialContext = environment.getInitialContext();
Object ds = initialContext.lookup("YYY");
Subscribe to:
Posts (Atom)