5 Replies Latest reply on Aug 3, 2007 11:53 AM by wayne baylor

    JBoss and PostgreSQL 8.1

    Sascha Effert Newbie

      Hello,

      I am using jboss 4.2.1 with PostgreSQL 8.1. With this I tried to bouild an Entity Bean that stores informations about some servers. One of the fields is the IP of the server. It looks like this:


      @Entity
      @IdClass(ComponentPK.class)
      public class Server implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;

      private String hostname;

      private String ip;
      ....
      }


      I build a JSP creating such an Entity. Using it I get an Exception which ends with:

      java.sql.BatchUpdateException: Batch-Eintrag 0 insert into Server (hostname, ip, id, sessionId) values (albana, 192.168.65.68, NULL, 5) wurde abgebrochen. Rufen Sie getNextException auf, um die Ursache zu erfahren.


      O.K., it is german. Translated to english it means the following:


      java.sql.BatchUpdateException: Batch-Entry 0 insert into Server (hostname, ip, id, sessionId) values (albana, 192.168.65.68, NULL, 5) was canceled. Call getNextException to discover the reason.


      If I find out how to make JBoss english speaking I can also send the real error Message here.

      If I try to use the SQL there in my database I get a syntaxerror. This happens because ma database thinks that 192.168.65.68 is a number, and there is a problem with the second point for it. Is it my failure or a bug in JBoss? I think the SQL should be insert into Server (hostname, ip, id, sessionId) values ('albana', '192.168.65.68' , NULL, 5)

      If I change it to this the seconp problem is that the NULL as id is not allowed because it is a primary key. Is anyone using JBoss with Postgre and can tell me what is happening there?

        • 1. Re: JBoss and PostgreSQL 8.1
          wayne baylor Apprentice

          could you please post the code for ComponentPK. also,
          you only have 3 fields showing in your posted entity, but the insert has 4. could you also post the rest of the entity code.

          just going off of what you have posted i am wondering why you have @IdClass specified when there is only one field annotated with @Id.

          • 2. Re: JBoss and PostgreSQL 8.1
            Sascha Effert Newbie

            For sure I can. Sorry I copied only one of the ID Fields:

            Server.java:

            package de.douglas2a.components;

            import java.io.Serializable;
            import java.util.Set;
            import javax.persistence.Entity;
            import javax.persistence.GeneratedValue;
            import javax.persistence.GenerationType;
            import javax.persistence.Id;
            import javax.persistence.IdClass;
            import javax.persistence.ManyToMany;

            @Entity
            @IdClass(ComponentPK.class)
            public class Server implements Serializable {

            @Id
            @GeneratedValue(strategy = GenerationType.AUTO)
            private Long id;

            @Id
            private Long sessionId;

            private String hostname;

            private String ip;

            public Long getId() {
            return this.id;
            }

            public void setId(Long id) {
            this.id = id;
            }

            public Long getSessionId() {
            return sessionId;
            }

            public String getHostname() {
            return hostname;
            }

            public void setHostname(String hostname) {
            this.hostname = hostname;
            }

            public String getIp() {
            return ip;
            }

            public void setIp(String ip) {
            this.ip = ip;
            }

            public Server() {
            }

            public Server(Long sessionId) {
            this.sessionId = sessionId;
            }

            @Override
            public int hashCode() {
            int hash = 0;
            hash += (this.id != null ? this.id.hashCode() : 0);
            return hash;
            }

            @Override
            public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Server)) {
            return false;
            }
            Server other = (Server)object;
            if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
            return true;
            }

            @Override
            public String toString() {
            return "de.upb.vdrive.components.Server[id=" + id + "]";
            }
            }


            ComponentPK.java:

            package de.douglas2a.components;

            public class ComponentPK implements java.io.Serializable {

            private Long id;

            private Long sessionId;

            public ComponentPK() {
            }

            public ComponentPK(Long id, Long sessionId) {
            this.id = id;
            this.sessionId = sessionId;
            }

            public boolean equals(Object obj) {
            if (obj == this) return true;
            if (!(obj instanceof ComponentPK)) return false;
            ComponentPK pk = (ComponentPK)obj;
            if (!id.equals(pk.id)) return false;
            if (!sessionId.equals(pk.sessionId)) return false;
            return true;
            }

            public int hashCode( ) {
            int id = this.id.intValue();
            int sid = this.sessionId.intValue();
            return id + Integer.reverse(sid);
            }
            }


            If needed I can also post my config-files for configuring Postgre as Datasource...

            • 3. Re: JBoss and PostgreSQL 8.1
              Sascha Effert Newbie

              Very Strange... As soon as I remove the line

              @IdClass(ComponentPK.class)

              and the @Id before the second part of the key everything runs fine. And the same happens with HSQL as DB: I get Exceptions if I use a composed primary key, but there is no error with a normal primary key.

              The Problem is: I really need a composed primary key...

              • 4. Re: JBoss and PostgreSQL 8.1
                Sascha Effert Newbie

                Hmmm... Much more strange: if I keep my compound primary key but remove the line

                @GeneratedValue(strategy = GenerationType.AUTO)

                and calculate both parts of the key myself, then everything is o.k.

                I do not understand what is going wrong there... Is there a Problem using @GeneratedValue together with a compound primary key?

                • 5. Re: JBoss and PostgreSQL 8.1
                  wayne baylor Apprentice

                  yes, as far as i know Hibernate will not populate the value when used with a composite PK. To enable that behavior you have to define some custom classes, etc..

                  We regard this as an extremely strange thing to want to do. If you have a generated surrogate key, why not just make it be the primary key?

                  However, if you must do this, you can do it by writing a CompositeUserType for the composite identifier class, and then defining a custom IdentifierGenerator that populates the generated value into the composite key class.

                  http://www.hibernate.org/117.html?cmd=prntdoc
                  Under the question: "I have a composite key where one column holds a generated value...?"