Bug 431 : Wrong SQL generation with OneToMany relations and subclassing
Priority 
High
Reported Version 
 
Logged By 
Angel Retamar
Status 
Fixed
Fixed Version 
3.x
Assigned To 
 
Product 
Ebean - core
Duplicate Of 
 
Created 
11/11/2012
Updated 
11/11/2012
Type 
Bug
 
Attachments 
No attachments

Given next three entity classes:

@Entity
class Wheel {

@Id
Long id

@ManyToOne(optional=false)
Car owner
}

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type")
@DiscriminatorValue("car")
class Car {

@Id
String plateNo

@OneToMany(cascade=CascadeType.ALL)
List wheels
}

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type")
@DiscriminatorValue("truck")
class Truck extends Car {

Long load;
}


If next code is executed...

Truck truck = new Truck();
truck.setPlateNo("foo")

Wheel wheel = new Wheel();
wheel.setOwner(truck);

Ebean.save(truck);

//This save() works ok...

//But if then is added one more wheel
wheel = new Wheel()
wheel.setOwner(truck)

//And save() is called again
Ebean.save(truck);

//Then an exception is raised:

javax.persistence.PersistenceException: Query threw SQLException:usuario no tiene privilegios suficientes o objeto no encontrado: TRUCK_PLATE_NO
Bind values:[null]
Query was:
select t0.id c0
from wheel t0
where truck_plate_no=? and not (t0.id in (?) )

Note that fk name generated is "truck_plate_no" when "car_plate_no" should be used.

Error lays here;

In AnnotationAssocManys class, method: private void read(DeployBeanPropertyAssocMany prop) ... fk prefix generated at line 156 is:

String fkeyPrefix = null;
if (nc.isUseForeignKeyPrefix()){
fkeyPrefix = nc.getColumnFromProperty(descriptor.getBeanType(), descriptor.getName());
}

// Use the owning bean table to define the join
BeanTable owningBeanTable = factory.getBeanTable(descriptor.getBeanType());
owningBeanTable.createJoinColumn(fkeyPrefix, prop.getTableJoin(), false);

Note that ever is used "descriptor.getName()" to generate fk prefix...subclass name is used even if property belongs to superclass.

 
woResponse

Upload a file