Sun Application Server 8.2 + Hibernate Gotchas!

For Hibernate to work in Sun Application Server 8.2, we need to change Sun App’s server.policy. Here is what we have to add:


grant {
  // Permission for cglib part of Hibernate framework
  permission java.lang.RuntimePermission "getProtectionDomain";
  permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
};

In Sun Application Server 8.2 Platform Edition, this server.policy file is located at [Sun App Install Path]/domains/[domain-name]/config folder. After applying this modification, the server needs to be restarted.

In Sun Application Server 8.2 Enterprise Edition, server.policy files exist in the domain and in each node-agents. Any alterations to configuration files, such as server.policy need to be made in the domain, rather than the node agent. If they are made in the node agent, then they will be overwritten the next time that the server instance is restarted. For making a change to a config file the approach would be:
1. stop the server instances
2. stop the node agent
3. stop the domain
4. change the config file(s) in the domain
5. start the domain
6. start the node agent (The node agent will start the server instances.)

If changes in the domain’s server.policy doesn’ get replicated in the node agents, you can use the ‘touch’ command on the file in the domain and restart the instance. ‘touch’ command updates the timestamp on the file. The node agent uses the timestamp on the file to work out whether it’s newer than in the server instance.

Failing to make changes in the server.policy will cause
java.lang.NoClassDefFoundError (without any detail which library it’s missing)

How to use Oracle XMLType in Hibernate

  • Edit your Hibernate Mapping XML

<?xml version=”1.0″?>
<!DOCTYPE hibernate-mapping PUBLIC
“-//Hibernate/Hibernate Mapping DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd” >

<hibernate-mapping>

<class
name=”mypackage.MyClass”
table=”MYTABLE”
>

<id
name=”myId”
type=”java.lang.Long”
column=”MY_ID”
>

<generator class=”sequence”>
<param name=”sequence”>my_id_seq</param>
</generator>

</id>

<property
name=”myXml”
type=”mypackage.HibernateXMLType”
column=”MY_XML”
/>

</class>

</hibernate-mapping>

  • Edit your Java Persistence Object

public class MyClass implements Serializable {

private Document myXml;

public Document getMyXml() {
return myXml;
}

public void setMyXml(Document messageXml) {
this.myXml = messageXml;
}
}

  • Add required JARS
    You would need xdb.jar, xmlparserv2.jar, and ojdbc14.jar

Continue reading

Hibernate: Ordering with SQL function

If you need to do ordering by an SQL function, eg:

ORDER BY YOUR_SQL_FUNCTION(your_table.your_column) ASC

This is what you need to do so that you can use Hibernate for this kind of ordering:

1. In your Hibernate mapping xml, add the following:

<class
name=”…”
table=”…”
>

<subselect>
<![CDATA[SELECT your_table.*, YOUR_SQL_FUNCTION(your_table.your_column) AS temp_column_name FROM YOUR_TABLE your_table]]>
</subselect>

<property
name=”javaFieldName”
type=”java.lang.String”
column=”temp_column_name”
update=”false”
insert=”false”
>
</property>

</class>

2. In your persistent Java class, add getter and setter for javaFieldName.

private String javaFieldName;

public String getJavaFieldName() {
return javaFieldName;
}

public void setJavaFieldName(String javaFieldName) {
this.javaFieldName = javaFieldName;
}

3. In your DAO class:

criteria.addOrder(Order.asc(”javaFieldName”));