• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dupicate Insert Happening

 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Chaminda Amarasinghe
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Amandeep Singh
Ranch Hand
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 850
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry the logs now have been deleted, will look in future. if the issue happened.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic