aspose file tools*
The moose likes Object Relational Mapping and the fly likes Problem with @Column annotation or corresponding xml tag if used with @Id annotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem with @Column annotation or corresponding xml tag if used with @Id annotation " Watch "Problem with @Column annotation or corresponding xml tag if used with @Id annotation " New topic
Author

Problem with @Column annotation or corresponding xml tag if used with @Id annotation

Nitin Takale
Greenhorn

Joined: Nov 30, 2006
Posts: 26
Hi All,

I've a table named MyEmployee and it has columns id,first,last,sal it is created in Access.

I'm using JPA with Toplink as provider.

now my code for employee entity is as under....

package com.seed;

import javax.persistence.*;
import javax.persistence.spi.*;


@Table(name="MyEmployee")
@Entity
public class Employee {
public Employee(){}

private int empId;
private String firstName,lastName;
private double sal;

// @GeneratedValue(strategy=GenerationType.AUTO)
@Id
// @Column(name="id")
public int getEmpId() {
return empId;
}

public void setEmpId(int empId) {
this.empId = empId;
}

@Column(name="first")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}


@Column(name="last")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}


public String toString(){
return "Employee Information is \n empId is "+ empId + " FirstName is "+firstName+" lastName is "+lastName+" salary is "+sal;
}

}


In the above code I'm commenting @Column and @GeneratedValue annotations

Now my orm.xml is.....

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0">
<entity class="com.seed.Employee">
<table name="MyEmployee"/>
<attributes>
<id name="empId">
<column name="id"/>
<generated-value strategy="AUTO"/>
</id>
<basic name="empId" fetch="LAZY">
<column name="id"/>
</basic>
<basic name="firstName" fetch="LAZY">
<column name="first"/>
</basic>
<basic name="lastName" fetch="LAZY">
<column name="last"/>
</basic>
</attributes>
</entity>
</entity-mappings>


and my client code is... I'm using plain java SE client. I've set the appropriate class paths and paths....


package com.seed.client;
import com.seed.*;
import javax.persistence.*;
import javax.persistence.spi.*;


public class Client
{
public static void main(String[] args)
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyEmployees");
EntityManager em = emf.createEntityManager();


// insert a new record in database
Employee emp = new Employee();
emp.setEmpId(125);
emp.setFirstName("Rahul");
emp.setLastName("Shah");
emp.setSal(80000.90);
EntityTransaction et=em.getTransaction();
et.begin();
em.persist(emp);
em.flush();
et.commit();

//After commit the transaction is closed
System.out.println("Checking the transaction is it active or not "+et.isActive());

//Now Find existing record
System.out.println("\n\nNow Finding the same record");
Employee emp1 = (Employee)em.find(Employee.class,125);
System.out.println("\n\n\n\n"+emp1);

emp1.setLastName("Barve");
System.out.println("After changing the value emp1 is \n\n"+emp1 + "\n and emp is \n\n "+emp);
System.out.println("Now Refreshing the data from database ");
em.refresh(emp1);
System.out.println("After Refreshing the data employee info is "+emp1);

System.out.println("\n\n\t\tClosing EntityManager \n\n");

em.close();

System.out.println("\n\nEntity manager is it open "+em.isOpen());
System.out.println("\n\nEntity manager Factory is it open "+emf.isOpen());

System.out.println("\n\n\t\tClosing EntityManager Factory\n\n");
emf.close();
System.out.println("\n\nEntity manager is it open "+em.isOpen());
System.out.println("\n\nEntity manager Factory is it open "+emf.isOpen());

}
}



In this code... the insertion of entity happens properly but it throws exception when I'm calling find method. I'm getting runtime exception. It says NoSuchMethodException .... getEmpId()


So..... is it like... if the primary key column name and primary key field name in entity class is not matching then it won't work.. I don't think it should happen but it is happening with me... please check this out if you are also getting the same error....

See I'm saying insert is working only finder is not working.... what could be reason.

Please help me out in this.

Thanks

nitin


Nitin<br /> <br />SCJP 6.0<br />SCBCD 5.0
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Very odd. It would have had to use the getEmpId method for the insert, so odd that insert works and read does not. I can't see it having anything to do with the @Column, does the issue not occur without this?

Please include the full exception stack trace.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
Ok, say an issue with your XML. You have the same mapping defined twice, empId defined as an <id> and as a <basic>, you can only map each attribute once, remove the <basic>.
Nitin Takale
Greenhorn

Joined: Nov 30, 2006
Posts: 26
Originally posted by James Sutherland:
Ok, say an issue with your XML. You have the same mapping defined twice, empId defined as an <id> and as a <basic>, you can only map each attribute once, remove the <basic>.




Thanks James got it...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with @Column annotation or corresponding xml tag if used with @Id annotation