File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Dupicate Insert Happening Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Dupicate Insert Happening" Watch "Dupicate Insert Happening" New topic
Author

Dupicate Insert Happening

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
here is my entity class-

import java.io.Serializable;

import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import javax.persistence.Column;


@Entity
//@Table(name="Car")
//@IdClass(CarPK.class)
public class Car implements Serializable{


/**
*
*/
private static final long serialVersionUID = 1L;
private String horn;
private String id;
private String steering;
private String chasis;

public Car(){
}

public Car(String horn, String steering, String chasis){
this.horn = horn;
this.steering = steering;
this.chasis = chasis;
}

public Car(String horn){
this.horn = horn;
}

@EmbeddedId
protected CarPK carPK;

@Embedded
protected CarAddress carAddress;
}

Mine CarPK class

import java.io.Serializable;

import javax.persistence.Embeddable;

@Embeddable
public class CarPK //implements Serializable
{

private static final long serialVersionUID = 1L;
private String horn;
private String id;
private String steering;
private String chasis;

public CarPK(){}

public boolean equals(Object other){
System.out.println(" equals method is called");
if (other instanceof CarPK){
final CarPK carPK = (CarPK)other;
return (carPK.equals(chasis) && carPK.equals(horn) && carPK.equals(steering) && carPK.equals(id));
}
else {
return false;
}
}

public int hashCode(){
return super.hashCode();
}

}


and when i try to execute my client code, it gives me exception like this in database-

[#|2008-11-16T15:52:11.753-0500|WARNING|sun-appserver9.1|oracle.toplink.essentials.session.file:/C:/Sun1/AppServer/domains/domain1/applications/j2ee-modules/text_ejb_simple1/-actionBazaar.transaction|_ThreadID=31;_ThreadName=p: thread-pool-1; w: 28;_RequestID=2894dd00-ac90-4936-8b6e-a2fd7c3f93b9;|
Local Exception Stack:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b04-fcs (04/11/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Column name 'ID' appears more than once times in the column list of an INSERT statement.
Error Code: -1
Call: INSERT INTO CAR (STEERING, CHASIS, ID, HORN, ID, STEERING, CHASIS, HORN, CITY, STATE, STREETLINE2, ZIPCODE, STREETLINE1, COUNTRY) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [null, null, null, black steerings, null, null, null, black steerings, null, null, null, null, null, null]
Query: InsertObjectQuery(Car@29e202)



please let me know what i am doing wrong...


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Chaminda Amarasinghe
Ranch Hand

Joined: May 17, 2006
Posts: 402
change the embeddable's id column name. keep in mind that there is only one table for 2 classes.

PS
there are 2 id fields, one in Car and other in CarPK, correct that by changing one of column name. I hope you no need id field in Car class since it already in CarPK
[ November 16, 2008: Message edited by: Chaminda Amarasinghe ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I agree with Chaminda. It's not even a duplicate insert. I think that the logged error message is explicit enough : Column name 'ID' appears more than once times in the column list of an INSERT statement. Call: INSERT INTO CAR (STEERING, CHASIS, ID, HORN, ID, STEERING, CHASIS, HORN, CITY, STATE, STREETLINE2, ZIPCODE, STREETLINE1, COUNTRY).

Same for steering, chasis and horn.
[ November 16, 2008: Message edited by: Christophe Verre ]

[My Blog]
All roads lead to JavaRanch
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
So it means i do have a option either to use

1) @EmbeddedId
protected CarPK carPK;

2) @Embeddable
public class CarPK //implements Serializable

if i use only 2) option,how @Embeddable attribute make's all the instance variable's as column's because i haven't yet embedded them using the @EmbeddedId.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

No, it means that you must not have the same fields both in the entity and in the embedded class. Look at your Car class and your CarPK class. They both have the same fields (horn, id , steering, chasis). Embedded fields must be only in the embedded class.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Thanks for the reply.

if the common instance variable's in Car and CarPK class cause duplicate column's insert statment.

Then when i deploy the jar file, why does Oracle Toplink, does not create the duplicate column field's in database.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did you check your server's log ? You should have something like :

Exception Description: Entity class [class entities.Car] has both an @EmbdeddedId (on attribute [carPK]) and an @Id (on attribute [id]. Both id types cannot be specified on the same entity. -- Exception [TOPLINK-28018] (Oracle TopLink Essentials - 2.0.1 (Build b04-fcs (04/11/2008)))
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
Sorry the logs now have been deleted, will look in future. if the issue happened.
 
Don't get me started about those stupid light bulbs.
 
subject: Dupicate Insert Happening
 
Similar Threads
Beans binding with local variable
Why i am getting no primary key specified exception?
oracle.toplink.essentials
Apache Derby Sql Statement's
SQLSyntaxErrorException: Attempt to modify an identity column 'ID'