Results 1 to 4 of 4
  1. #1
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation Having trouble using annotation with Hibernate

    Hi Forum

    Im new to using Hibernate and having trouble mapping a class that extends from another class.Im trying to get data from to table in my database that are connected using One to One relationship. It keeps giving me this error Unable to define/override @Id(s) on a subclass: com.TimeKeeperExpress.model.HourlyEmployee :confused:


    here my code

    here my base class
    Java Code:
    package com.TimeKeeperExpress.model;
    
    import java.io.Serializable;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Inheritance;
    import javax.persistence.InheritanceType;
    import javax.persistence.Table;
    
    
    @Entity
    @Table(name="tbl_Employee")
    @Inheritance(strategy=InheritanceType.JOINED)
    public class Employee implements Serializable
    {
      
    /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    //private attributes
      private int empId;
      private String lastName;
      private String firstName;
      private String email;
      private String password;
      private String userName;
      
      //constructors
      public Employee()
      {
    	  
      }
      //--------------------------
      
      public Employee(int empId, String lastName, String firstName, String email,
    		String password, String userName) 
      {
    	
    	this.empId = empId;
    	this.lastName = lastName;
    	this.firstName = firstName;
    	this.email = email;
    	this.password = password;
    	this.userName = userName;
      }
    
    
    
    //class methods
      
      //get set methods
      public void setEmpId(int empId) 
      {
    	this.empId = empId;
      }
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name="Emp_Id")
      public int getEmpId() 
      {
    		return this.empId;
      }
      //------------------------------
      public void setLastName(String lastName) 
      {
    	this.lastName = lastName;
      }
      
      @Column(name="last_name")
      public String getLastName() 
      {
         return lastName;
      }
      //---------------------------------
      public void setFirstName(String firstName) 
      {
    	this.firstName = firstName;
      }
      @Column(name="first_name")
      public String getFirstName() 
      {
    	return this.firstName;
      }
      //---------------------------------
      public void setEmail(String email) 
      {
    		this.email = email;
      }
      @Column(name="email")
      public String getEmail() 
      {
    	return this.email;
      }
      //----------------------------------
      public void setPassword(String password) 
      {
    	this.password = password;
      }
      @Column(name="password")
      public String getPassword() 
      {
    	return this.password;
      }
      //------------------------------------
      public void setUserName(String userName) 
      {
    	this.userName = userName;
      }
      @Column(name="userName")
      public String getUserName() 
      {
        return this.userName;
      }
      //-------------------------------------
    //to string
    @Override
    public String toString()
    {
    	return String.format("Employee [getEmpId()=%s, getLastName()=%s, getFirstName()=%s, getEmail()=%s, getPassword()=%s, getUserName()=%s]",
    					getEmpId(), getLastName(), getFirstName(), getEmail(),
    					getPassword(), getUserName());
    }
    //the equals method
    //-------------------------------------
    public boolean equals(Object theOther)
    {
    	if(theOther == null)
    	{
    		return false;
    	}
    	else if(getClass() != theOther.getClass())
    	{
    		return false;
    	}
    	else
    	{
    		Employee otherEmployee = (Employee)theOther;
    		return(getEmpId()==(otherEmployee.getEmpId()));
    	}
    }
    //-------------------------------------
    
    	
    	
    }//end of class Employee
    #########################
    down below is the HourlyEmployee that extends Employee
    #########################
    Java Code:
    package com.TimeKeeperExpress.model;
    
    import java.io.Serializable;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.OneToOne;
    import javax.persistence.PrimaryKeyJoinColumn;
    import javax.persistence.Table;
    
    
    
    @Entity
    @Table(name="tbl_hrlyEmployee")
    @PrimaryKeyJoinColumn(name="Emp_ID")
    public class HourlyEmployee extends Employee
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	//private attributes
    	private int hrlyID;
    	
    	//public attributes
    	
    	//constructor(s)
    	public HourlyEmployee()
    	{
    		super();
    	}
    	//--------------------------
    	public HourlyEmployee(int hrlyID) 
    	{
    		this.hrlyID = hrlyID;
    		// TODO Auto-generated constructor stub
    	}
    	//-----------------------
    	public HourlyEmployee(int empId, String lastName, String firstName,
    			String email, String password, String userName, int hrlyID) 
    	{
    		super(empId, lastName, firstName, email, password, userName);
    		this.hrlyID = hrlyID;
    		// TODO Auto-generated constructor stub
    	}
    	//class methods
    	
    	
    	
    
    	//get set methods
    	public void setHrlyID(int hrlyID) 
    	{
    		this.hrlyID = hrlyID;
    	}
    	@Id
    	@Column(name="hr_Emp_id",unique=true)
    	public int getHrlyID() 
    	{
    		return this.hrlyID;
    	}
    	//----------------------------
    	
    	public String toString() 
    	{
    		return String.format("%s, HrlyEmpId[%d]",super.toString(),getHrlyID());
    	}
    	//--------------------------
    	public boolean equals(Object theObject)
    	{
    		if(theObject == null)
    		{
    			return false;
    		}
    		else if(getClass() == theObject.getClass())
    		{
    			return false;
    		}
    		else
    		{
    			HourlyEmployee theHrlyEmployee = (HourlyEmployee)theObject;
    			return (super.equals(theHrlyEmployee) && 
    					(getHrlyID() == theHrlyEmployee.getHrlyID()));
    		}
    	}
    	//-----------------------------
    	
    	
    }//end of class Hourly Employee
    What am I doing wrong i cant figure it out.
    Any Help would be much appreciated and thank you in Advance
    Last edited by ShinTec; 09-06-2010 at 03:31 AM. Reason: wrong class inserted

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    It keeps giving me this error
    please copy and paste here the FULL text of the error messages.

  3. #3
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    Java Code:
    19:10:51,078  INFO Version:15 - Hibernate Annotations 3.4.0.GA
    19:10:51,098  INFO Environment:560 - Hibernate 3.3.2.GA
    19:10:51,098  INFO Environment:593 - hibernate.properties not found
    19:10:51,108  INFO Environment:771 - Bytecode provider name : javassist
    19:10:51,108  INFO Environment:652 - using JDK 1.4 java.sql.Timestamp handling
    19:10:51,178  INFO Version:14 - Hibernate Commons Annotations 3.1.0.GA
    19:10:51,178  INFO Configuration:1474 - configuring from resource: /hibernate.cfg.xml
    19:10:51,178  INFO Configuration:1451 - Configuration resource: /hibernate.cfg.xml
    19:10:51,389  INFO Configuration:1589 - Configured SessionFactory: null
    19:10:51,389  INFO HibernateSearchEventListenerRegister:53 - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
    19:10:51,499  INFO AnnotationBinder:419 - Binding entity from annotated class: com.TimeKeeperExpress.model.Employee
    19:10:51,529  INFO EntityBinder:422 - Bind entity com.TimeKeeperExpress.model.Employee on table tbl_Employee
    Exception in thread "main" java.lang.ExceptionInInitializerError
    	at com.TimeKeeperExpress.util.HibernateUtil.<clinit>(HibernateUtil.java:21)
    	at com.TimeKeeperExpress.model.HrlyEmployeeManager.getHrlyEmpList(HrlyEmployeeManager.java:86)
    	at com.TimeKeeperExpress.test.TestEmployee.main(TestEmployee.java:30)
    Caused by: org.hibernate.AnnotationException: Unable to define/override @Id(s) on a subclass: com.TimeKeeperExpress.model.HourlyEmployee
    	at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1860)
    	at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1279)
    	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
    	at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
    	at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
    	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1333)
    	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    	at com.TimeKeeperExpress.util.HibernateUtil.<clinit>(HibernateUtil.java:18)
    	... 2 more
    19:10:51,579  INFO AnnotationBinder:419 - Binding entity from annotated class: com.TimeKeeperExpress.model.HourlyEmployee
    19:10:51,589  INFO EntityBinder:422 - Bind entity com.TimeKeeperExpress.model.HourlyEmployee on table tbl_hrlyEmployee
    Is say its caused by the hibernate.org.annotationException being thrown and when i went to check the api on the class AnnotationException it had no real useful information

  4. #4
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    oh figure it out It was the @Id in the hourlyEmployee class that was causing the error and in the @primaryKeyJoinColumn name attribute = to "Emp_id"
    so got rid of the @id in the hourlyEmployee class and changed the @primaryKeyJoinColumn name attribute to "hr_Emp_id" which was also in the hourlyEmployee class and it worked

Similar Threads

  1. @Override Annotation
    By Unsub in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 02:06 PM
  2. Replies: 2
    Last Post: 06-30-2007, 03:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •