Archive for the ‘Java’ Category

Hibernate (annotation): Error: TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing

Friday, March 27th, 2009

If you get an error like this one

Hibernate: Error: TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing..

It is probably because you did not annotate your ManyToOne with the Hibernate SAVE_UPATE Cascade annotation:

@ManyToOne()
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
public Category getCategory() {
        return category;
}

Note, putting only

@ManyToOne(cascade = {CascadeType.MERGE,CascadeType.PERSIST})
public Category getCategory() {

Will not resolve the issue.

Java: Hibernate 3.4 and SLF4J Impl Class not found and Singleton error

Friday, March 20th, 2009

Hibernate 3.4 does not have a SLF4J implementation, and if you download the latest implementation from http://www.slf4j.org/download.html you will get an imcompatible impl version (1.5.6 impl with the 1.5.2 api from Hibernate).

Just do the following:

  1. Remove all slf4j*.jar from your lib.
  2. Add the the latest sfl4j-api (i.e. slf4j-api-1.5.2)
  3. Add the latest impl slf4j-log4j12-1.5.6-.jar

NOTE: If you use Hibernate, make sure to use the slf4j-log4j*.jar, otherwise, changing the log4j.properties will have no effect. (see Turn off logging from log4j and slf4j)

Java: ActiveMQ in-VM Configuration, Initialization, and Usage

Saturday, January 24th, 2009

Creating a Connection Factory (Programmatic):

import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.Connection;
import javax.jms.Session;
...
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
...
Connection connection = factory.createConnection();
...
connection.start();
...
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Java: Open Source JMS

Saturday, January 24th, 2009

I am looking at an in-VM (or in-Application) JMS implementations. ActiveMQ seems to have good support for in-vm JMS.

ActiveMQ

ActiveMQ: is an open source, Apache 2.0 licensed Message Broker and JMS 1.1 implementation which integrates seamlessly into Geronimo, light weight containers and any Java application. JMS 1.1 / J2EE 1.4 compliant, full support for JCA resource adaptors, upport for transient, persistent, transactional and XA messaging, supports in-VM, TCP, SSL, NIO, UDP, multicast, JGroups and JXTA transports, REST API, Streamlets to support web streaming support, in memory JMS provider, ideal for unit testing, JSR 77 / 88 support for easy deployment & management & hot deployment, rules based message routing via Drools.

In-VM URI example:

vm://broker1?marshal=false&broker.persistent=false&async=false

See Also:

Other JMS Implementations

  • mom4J: is a 100% pure Java® implementation of the Java Message Service (JMS)® specification, published by Sun Microsystems®. LGPL
  • OpenJMS - Supports both Point-to-Point and publish-subscribe messaging models, Guaranteed delivery of messages, synchronous and asynchronous message delivery, persistence using JDBC, Local transactions, message filtering using SQL92-like selectors, Applet support, integrates with Servlet containers, and support for RMI, TCP, HTTP and SSL protocol stacks.

See also:

jDom: Converting from JDOM to DOM

Saturday, January 24th, 2009
public org.w3c.dom.Document convertToDOM(org.jdom.Document jdomDoc)
     throws JDOMException {

     DOMOutputter outputter = new DOMOutputter();
     return outputter.output(jdomDoc);
}

public org.w3c.dom.Element convertToDOM(org.jdom.Element jdomElement)
     throws JDOMException {
     DOMOutputter outputter = new DOMOutputter();
     return outputter.output(jdomElement);
}

public org.w3c.dom.Attr convertToDOM(org.jdom.Document jdomAttribute)
     throws JDOMException {
     DOMOutputter outputter = new DOMOutputter();
     return outputter.output(jdomAttribute);
}

Source: Tip: Converting from JDOM

Java: Get Element Type From A Typed Array

Sunday, December 21st, 2008
  System.out.println("" +  Long[].class.getComponentType() == Long.class);
  //will print "true"

Groovy: Embedding Groovy in Java

Saturday, October 4th, 2008

Embedding Groovy in Java

Java: ROME to parse, generate and publish RSS and Atom Feeds

Saturday, October 4th, 2008

Rome project page

Very good lib for parsing, generating and publishing RSS and Atom feeds.

Java: HTML Parsers

Friday, October 3rd, 2008
  • HTML Parser: Mature, proven. But, weak doc, old (2006), kind of strange API
  • Jericho: Seems to be newer, and have simple API, optimize for large doc (tree-event hybrid approach)

Other Java HTML Parsers

Java: HTTP Client (Apache)

Friday, October 3rd, 2008

Http Client from Apache

Features:

  • Standards based, pure Java, implementation of HTTP versions 1.0 and 1.1
  • Full implementation of all HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE) in an extensible OO framework.
  • Supports encryption with HTTPS (HTTP over SSL) protocol.
  • Granular non-standards configuration and tracking.
  • Transparent connections through HTTP proxies.
  • Tunneled HTTPS connections through HTTP proxies, via the CONNECT method.
  • Transparent connections through SOCKS proxies (version 4 & 5) using native Java socket support.
  • Authentication using Basic, Digest and the encrypting NTLM (NT Lan Manager) methods.
  • Plug-in mechanism for custom authentication methods.
  • Multi-Part form POST for uploading large files.
  • Pluggable secure sockets implementations, making it easier to use third party solutions
  • Connection management support for use in multi-threaded applications. Supports setting the maximum total connections as well as the maximum connections per host. Detects and closes stale connections.
  • Automatic Cookie handling for reading Set-Cookie: headers from the server and sending them back out in a Cookie: header when appropriate.
  • Plug-in mechanism for custom cookie policies.
  • Request output streams to avoid buffering any content body by streaming directly to the socket to the server.
  • Response input streams to efficiently read the response body by streaming directly from the socket to the server.
  • Persistent connections using KeepAlive in HTTP/1.0 and persistance in HTTP/1.1
  • Direct access to the response code and headers sent by the server.
  • The ability to set connection timeouts.
  • HttpMethods implement the Command Pattern to allow for parallel requests and efficient re-use of connections.
  • Source code is freely available under the Apache Software License.