4 Replies Latest reply on May 16, 2005 10:17 AM by Ronald Hatcher

    Annotations in the head of the entity?

    Piero Sartini Newbie

      hello,

      i have to define an entity like this:

      public class Address implements Serializable {
       private long id;
       private String blah;
       ...
       @Id(generate = GeneratorType.AUTO)
       public long getId() {
       return id;
       }
       public void setId(long id) {
       this.id = id;
       }
       ...
      }
      


      in many tutorials the following syntax is used, which is - i think - much easier. one important point is code generation - eclipse for example generates the getters and setters for me - i just have to look at the top of the class to see everything i need when it comes to relations etc.
      public class Address implements Serializable {
       @Id(generate = GeneratorType.AUTO)
       private long id;
       private String blah;
       ...
       //getters and setters
       ...
      }
      


      you use this syntax in some of your examples here in the forum. maybe just to reduce the code size.. dont know. if i try it, it comes to an null pointer exception:

      java.lang.NullPointerException
       at org.hibernate.mapping.PersistentClass.createPrimaryKey(PersistentClass.java:266)
       at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:442)
       at org.hibernate.cfg.AnnotationConfiguration.addAnnotatedClass(AnnotationConfiguration.java:94)
       at org.hibernate.cfg.AnnotationConfiguration.addAnnotatedClasses(AnnotationConfiguration.java:62)
       at org.jboss.ejb3.entity.HibernateSessionFactory.createSessionFactory(HibernateSessionFactory.java:196)
       at org.jboss.ejb3.Ejb3Module.createHibernateSessionFactory(Ejb3Module.java:256)
       at org.jboss.ejb3.Ejb3Module.startService(Ejb3Module.java:263)
       at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:272)
       at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:222)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)
       at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
       at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
       at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
       at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
       at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:897)
       at $Proxy0.start(Unknown Source)
       at org.jboss.system.ServiceController.start(ServiceController.java:418)
       at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)
       at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
       at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
       at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
       at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
       at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:177)
       at $Proxy16.start(Unknown Source)
       at org.jboss.ejb3.EJB3Deployer.start(EJB3Deployer.java:166)
       at org.jboss.deployment.MainDeployer.start(MainDeployer.java:964)
       at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:775)
       at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:738)
       at sun.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)
       at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
       at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:121)
       at org.jboss.mx.server.Invocation.invoke(Invocation.java:74)
       at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:127)
       at org.jboss.mx.server.Invocation.invoke(Invocation.java:74)
       at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
       at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
       at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:177)
       at $Proxy8.deploy(Unknown Source)
       at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymentScanner.java:325)
       at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentScanner.java:483)
       at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.doScan(AbstractDeploymentScanner.java:204)
       at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.loop(AbstractDeploymentScanner.java:215)
       at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.run(AbstractDeploymentScanner.java:194)
      


      sorry if this is nothing new. its hard to find information like this in the forum or jira. is this planned in future releases, is it something that should work already or is it just something that will never come?

        • 1. Re: Annotations in the head of the entity?
          Bernd Dutkowski Newbie

           

          @Entity(access=AccessType.FIELD)
          public class Address implements Serializable {
           @Id(generate = GeneratorType.AUTO)
           private long id;
           private String blah;
           ...
           //getters and setters
           ...
          }
          



          • 2. Re: Annotations in the head of the entity?
            Ronald Hatcher Newbie

            Taking this example

            @Entity(access=AccessType.FIELD)
            public class Client implements java.io.Serializable {
            
             static final long serialVersionUID = -6952495295103532929L;
             @Id(generate = GeneratorType.AUTO)
             private Long id;
             private Collection<Policy> policies;
            
             public Long getId() {
             return id;
             }
            
             public void setId(Long id) {
             this.id = id;
             }
            
             public void addPolicy(String product) {
             if (policies == null) policies = new ArrayList<Policy>();
             Policy policy = new Policy();
             policy.setClient(this);
             policy.setProduct(product);
             policies.add(policy);
             }
            
             @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="client")
             public Collection<Policy> getPolicies() {
             return policies;
             }
            
             public void setPolicies(Collection<Policy> policies) {
             this.policies = policies;
             }
            }
            


            and

            @Entity(access=AccessType.FIELD)
            public class Policy implements java.io.Serializable {
            
             static final long serialVersionUID = 660645555211406110L;
            
             @Id(generate = GeneratorType.AUTO)
             private Long id;
             private Client client;
            
             public Long getId() { return id; }
            
             public void setId(Long id) { this.id = id; }
            
             @ManyToOne
             @JoinColumn(name = "client_id")
             public Client getClient() { return client; }
            
             public void setClient(Client client) { this.client = client; }
            }
            


            was working the other way now produces
            org.hibernate.MappingException: Could not determine type for: java.util.Collection, for columns: [org.hibernate.mapping.Column(policies)]
            


            Is this aproblem with my association mapping? A Bug? or ...

            Thanks

            • 3. Re: Annotations in the head of the entity?
              Emmanuel Bernard Master

              You must stay consistent,
              If you use access = FIELD, all you annotations have to be on fields (not on properties).