Weblog

All | Personal | Sun
« Previous page | Main | Next page »
20060216 Thursday February 16, 2006

Where is the NetBeans with JavaEE5 support avalaible? I have seen some e-mails on mailing lists that NetBeans JavaEE daily builds aren't avalaible on netbeans.org. It's true and this issue should be already resolved. However, forget for Java EE 5 builds since these builds will not be avalaible any more, you should use 5.5 release from now.

What's happened with old builds and what's are new ones?
The development of the JavaEE5 stuff has been done on javaee5 branch. The builds from this branch was published on pages as JavaEE5 release. We created new branch with name release55 from javaee5 and merged some fixies and changes in J2EE modules from trunk. These, changes look horrible but it seems that everything was done successfully and we can start to implement new features on this branch. The upcomming NetBeans 5.5 release will be built from this branch. Posted by pblaha ( Feb 16 2006, 01:12:14 PM CET ) Permalink Comments [4]

20060215 Wednesday February 15, 2006

How to use date in persistence In today's post I would like to describe how to use date data type in persistence class and then in queries. We very often use java.util.Date or java.util.Calendar objects in Java, these types can be used in persistence as well. In persistence class you can use Temporal annotation for specifying how the date property should be persisted. Without using Temporal annotation the property is persisted as TIMESTAMP. The TemporalType defines the mapping for temporal type, i.e. DATE, TIME, TIMESTAMP. The example is below:

     @Temporal(TemporalType.TIME)
     public Date getDueTime(){
       return dueDate;
     }
  
In this example dueDate will be persisted as java.sql.Date. Now, this property will be used in query. We should use setParameter in Entitymanager class and specify TemporalType for query like:
   List currentPlans = em.createQuery("SELECT a FROM Plan a WHERE a.creationTime  <= :time").
         setParameter("time", new Date(), TemporalType.TIME).getResultList();
   
Posted by pblaha ( Feb 15 2006, 09:19:49 AM CET ) Permalink

20060209 Thursday February 09, 2006

New code completion for Entity class in NetBeans The new preview release of the NetBeans 5.5 will be released very soon. This new version is focused on support for Java EE5 especially. This changes require new features in code completion as well. We plan to support code completion for almost all annotations for persistance. For instance, you will be able to see tables that are in database that is specified in persistence file and the connection is registered in NetBeans. See snapshot:

The latest builds of NetBeans 5.5 with JavaEE5 support you can download from www.netbeans.org pages. There, click Downloads link and Development Builds of Upcoming Releases. On next page select Java EE 5 item for release version. Posted by pblaha ( Feb 09 2006, 01:11:08 AM CET ) Permalink Comments [3]

20060208 Wednesday February 08, 2006

Using enumeration in Entity class In 5.0, the Java programming language gets support for enumerated types. In today's post I would like to show how to use enum in Entity classes. We should create new enumerated type firts:

    public enum AdvertisementState { OPEN, CLOSE, RESRVED}
  
This enum represents status of some advertisement. Then, we can create Entity class that represents advertisement:
  @Entity()
  public class Advertisement implements Serializable {
  private AdvertisementState state;

    @Enumerated(EnumType.ORDINAL)
    public AdvertisementState getState() {
        return state;
    }
You can use ORDINAL or STRING type. When you use ORDINAL type then columnt type is used INTEGER and for STRING the VARCHAR is used. Now, we can use the enum type for finding advertisements with OPEN status:
  em.createQuery("SELECT a FROM Advertisement a WHERE a.state = :state").
                  setParameter("state", AdvertisementState.OPEN).
                  setMaxResults(count).getResultList();
Using enum type is very easy, isn't it? Posted by pblaha ( Feb 08 2006, 12:08:20 AM CET ) Permalink

20051221 Wednesday December 21, 2005

Primary key generation in EJB 3.0blo I and John Jullion-Ceccarelli wrote a tutorial about primary key generation in EJB 2.1. It was very pretty painful stuff, you should create table with appropriate data type column, use Object primary key and other. In EJB 3.0 is generation of PK different, it's easy to use. Let's go through all options that are for this in EJB 3.0. Primary key you can define in Entity bean with @Id annotation. There are five options for GeneratorType: NONE, AUTO, IDENTITY, SEQUENCE and TABLE. First one is NONE, it means that application is responsible for generation of primary key. Next one is AUTO that leaves the job to the container. This strategy indicates that a persistence provider should pick up appropriate strategy according to the database. It means that you need not setup your id and container generates it. I would like to describe SEQUENCE and TABLE strategy in more details below. SEQUENCE or IDENTITY strategy use a database sequence or identity column. I will show this strategy for Oracle database. We need to create new sequence with this command:

    CREATE SEQUENCE EMPL_ID INCREMENT BY 1 START WITH 100;
  
Then, this sequence can be used in entity bean like this:
     @Id(generate=GeneratorType.SEQUENCE, generator="EMPL_GEN")
     @SequenceGenerator(name="EMPL_GEN",sequenceName="EMPL_ID")
  
The sequenceName attribute specifies name of sequnce object in database. For TABLE strategy we should create table in database where primary keys will be stored:
     CREATE TABLE GEN_ID(
     GEN_KEY VARCHAR(20),
     GEN_VALUE INTEGER,
     PRIMARY KEY(GEN_KEY))
  
Then you can use this table in entity bean with TABLE generator strategy:
   @Id(generate=GeneratorType.TABLE,generator="ORDER_GEN")
    @Column(name="ID")
    @TableGenerator(name="ORDER_GEN",pkColumnName="GEN_KEY",
            pkColumnValue="ORDER_ID",allocationSize=1,
            initialValue=1,valueColumnName="GEN_VALUE",
            table=@Table(name="GEN_ID"))
 
All attributes are very intuitive and can be guessed from this sample. 
Generation of primary key in EJB 3.0 is simple, isn't it? Posted by pblaha ( Dec 21 2005, 06:07:37 PM CET ) Permalink

20051219 Monday December 19, 2005

EJB 3.0 client in J2SE project Today, I would like to show how you can create client for EJB 3.0 in J2SE project. First, we will need to create a bean with remote interface. Since, we are using EJB 3.0 this is very simple. Create business interface with Remote annotation and then create bean's implementation class of your bean and specify JNDI name of this bean:

  @Stateless(name="ejb/ProcessHello")
  public class ProcessHelloBean implements org.netbeans.ProcessHello {

  public void String getHello(){
   ....
 
Now, we have two ways how to create client. First one is using Application Client Container (ACC) and second one is without that. What is advantage of the ACC? ACC can be seen as lightweight container that is responsible for security, naming, communication with application server and especially for injection. However, the worse of this approach is that you need run your application with appclient launcher. The client that uses ACC can be written like:
    @EJB(name="ejb/ProcessHello")
    private org.netbeans.ProcessHello hello;

    hello.getHello();
  
Then you need to run your client's jar with launcher appclient -client . The client launcher is located in bin directory of your application server. It's cool, but some smart user can ask what's about remote clients that aren't run on same machine as Glassfish? It's trivial task, you need to run package-appclient script that packs the application client container libraries and jar files into an appclient.jar file. This jar you can copied on remote server, unpack, change some properties and run your client. More info about this command is avalaible here. Now, let's develop EJB client without ACC. This client is similar as for EJB 2.1. It means first lookup bean interface and then invoke your business methods. You don't need to call create, ... and other lifecycle methods for the bean.
    InitialContext ctx = new InitialContext();
    Object obj = ctx.lookup("org.netbeans.ProcessHello");
    ProcessHello hello = (TestTableRemote)PortableRemoteObject.narrow(obj, ProcessHello.class);
    hello.getHello();
 
In this sample I used default JNDI name that is fully qualified classname of the remote business interface (3.0) or remote home interface (2.x). If you want to change the JNDI name use mappedName attribute for @Stateless. Posted by pblaha ( Dec 19 2005, 06:11:29 PM CET ) Permalink Comments [5]

20051215 Thursday December 15, 2005

Persistence in J2SE project The big advantage of the EJB 3.0 is using entity beans out of container. This new feature will allow to test your business objects outside of application server. I would like to show how you can write simple J2SE project that uses EntityManager in NetBeans 5.0. Let's create new J2SE project in NetBeans.

Posted by pblaha ( Dec 15 2005, 04:04:32 PM CET ) Permalink

20051214 Wednesday December 14, 2005

How to use EntityManager API in web module The javax.persistence.Entitymanager API is used for creating, finding and updating entity bean instances. I would like to show how EntityManager can be used in web module. I will focus only to Glassfish implementation. I know that Oracle App server uses a little bit different approach. They use EntityManager that is bind to java:comp/ejb/<module-name>/EntityManager.
First, some actions with EntityManager API are required to be used in a transaction, so the web module must manually demarcate the transaction using the UserTransaction API.
In objects that are managed by container like servlets, JSF beans and EJB bean you can use simple injection. Following sample explains how to use EntityManager in servlet:

     @PersistenceContext
     EntityManager em;
     @Resource
     UserTransaction tx;
     ....
     tx.begin();
     em.persist(new YourObject());
     tx.commit();
   
This approach can't be used in helper or business delegate classes. There, we should lookup theEntityManager and the UserTransaction using the JNDI binding:
    InitialContext ctx = new InitialContext();
    UserTransaction tx = (UserTransaction)ctx.lookup("UserTransaction");
    EntityManager em = em = (EntityManager) ctx.lookup("java:comp/env/persistence/EntityManager");
     tx.begin();
     YourObject o1 = em.merge(yourObject); // merge object to the new context
     em.remove(o1);
     tx.commit();
 
You'll need to define a persistence-context-ref for the component environment in which your class will run. It means add these elements in your web.xml file in web-app element:
  <persistence-context-ref>
  <persistence-context-ref-name>persistence/EntityManager</persistence-context-ref-name>
     <persistence-unit-name>unitName</persistence-unit-name>
  </persistence-context-ref>
 
Posted by pblaha ( Dec 14 2005, 05:47:22 PM CET ) Permalink Comments [1]

20051208 Thursday December 08, 2005

Develop Custom Realm in NetBeans I will write simple custom realm for Sun Application server in NetBeans. This realm will be used for authentication of users. I will implement very simple realm that will check password that are stored in Hashtable. I guess, extending this realm for using JDBC or other technology is easy task.
Implementation involves the following three steps:

The realm will be developed in NetBeans IDE. Final SimpleRealm project is avalaible here.Create new J2SE project and follow these steps: What's about debugging realm in NetBeans?
Start Application server in debug mode and put breakpoint in Realm class and attach debugger to port 9009. Then, you can login in your application and then debugg realm. ~ Posted by pblaha ( Dec 08 2005, 04:02:29 PM CET ) Permalink Comments [6]

20051206 Tuesday December 06, 2005

How to use CMP beans with generated primary key I saw a question about using CMP beans with generated primary key. Therefore, I decided to write one simple application that can explain how to use this feature. More info about generating primary key values in CMP beans is avalaible here here. I would like to note that the user should never rely on the internals of the container code. And if the user chooses to use a generated PK for CMP, it means they do not care about the value, and should not try to access it (other than as an Object).
In this post we will develop EJB module that creates product and purchase order. See Entity relationship diagram:

Posted by pblaha ( Dec 06 2005, 08:12:35 PM CET ) Permalink Comments [0]

20051205 Monday December 05, 2005

LDAP authentication in Sun Application server Today, I would like to describe the steps to enable LDAP authentication in web module that is deployed in Sun Application server. Authentication is the way an entity determines that another entity is who it claims to be.
Very important for understanding security for SJAS is Realm. A realm, also called a security policy domain or security domain, is a scope over which the server defines and enforces a common security policy. In practical terms, a realm is a repository where the server stores user and group information. The Application Server comes pre-configured with three realms: file (the initial default realm), certificate, and admin-realm. In this post we will add and setup new ldap realm.
I will use open source implementation of the Lightweight Directory Access Protocol server that is avalaible here.

Posted by pblaha ( Dec 05 2005, 06:48:07 PM CET ) Permalink Comments [9]

20051201 Thursday December 01, 2005

Hibernate plugin for NetBeans 5.0 Many developers don't like EJB, JDO technologies and they are using other object/relational persistance and query service for Java like Hibernate. Hibernate lets you develop persistant classes following common Java idiom. The plugin for Hibernate in NetBeans was missing. Recently, I have seen on project of my colleague.
Petr Zajac develops xdoclet plugin for NetBeans that enables Attribute-Oriented Programming for Java. The plugin parses your source code and generates many artifacts such as XML and other stuff. Based on this plugin he did Hibernate plugin.
This plugin allows you to create new POJO beans, add relationships, fields, ....code completion

Of course, the plugin generate all requested configuration files without your endeavour. The plugin is avalaible in SourceForge. You can download sources and nbms here
. I would like to show more plugin's functionality in some next post.
Posted by pblaha ( Dec 01 2005, 06:19:28 PM CET ) Permalink Comments [13]

20051130 Wednesday November 30, 2005

Web service client in EJB module in NetBeans 5.0 I'm working on one J2EE application that has web service client in EJB module. However, NetBeans 5.0 doesn't support web service client for EJB. Since NetBeans uses Ant as build tool is very easy to add this missing feature in NetBeans. We should changes build.properties and add new target in build.xml file. Let's to add support for web service client in NetBeans. These steps create Static generated web service client (JSR-101) in your EJB module:

Posted by pblaha ( Nov 30 2005, 06:08:25 PM CET ) Permalink Comments [2]

20051129 Tuesday November 29, 2005

Transactions and JMS I started to talk about JMS thence I might write a few sentences about transactions. Very often use case is that you deliver message and then this message is stored in database. How we can solve this. Should we use JTA for this?
I hope that reader knows that you mustn't use global transaction for consumer and producers. Why? Because, having all producers and all consumers participate in one global transaction would defeat the purpose of using a loosely coupled asynchronous messaging environment. JMS transactions follow the convention os separating the send operations from the receive operations. Which ways do we have for transactions with JMS?

Posted by pblaha ( Nov 29 2005, 07:55:00 PM CET ) Permalink Comments [2]

20051128 Monday November 28, 2005

Add other JMS subscriber in topic On of the big advance of the topic is that other subscriber can be added very easy. For instance, we have existing order application that have one ProcessOrder subscriber and we would like to add other one. In this post I will show how you can add new subscriber in NetBeans 5.0, which changes should be done:

Calendar

RSS Feeds

Search

Links

Navigation

Referers

Older blog entries