I dont know if this is a bug or there is something I am missing, but there is a problem when you try to use Inheritance.JOINED when the superclass has a composite primary key.
I have the following classes:
(Superclass)
#Entity
#Table(name = "tipos_opciones_misiones")
#IdClass(TipoOpcionMisionId.class)
#Inheritance(strategy = InheritanceType.JOINED)
#DiscriminatorColumn(name = "clase", discriminatorType = DiscriminatorType.STRING)
#ReadOnly
public abstract class TipoOpcionMision{
#Id
#ManyToOne
#JoinColumn(name="tipo_mision")
private TipoMision tipoMision;
#Id
#Column(name = "numero")
private int numero;
}
And a child class:
#Entity
#Table(name="tipos_misiones_opciones_comercio_compra")
#DiscriminatorValue("COMERCIO_COMPRA")
public class TipoOpcionMisionComercioCompra extends TipoOpcionMision{
#Column(name="valor")
double valor;
}
When I try to get a list on objects "TipoOpcionMision" the generated SQL ignores that there is a composite key [tipo_mision, numero] and it just uses "t1.numero = t0.numero". There should also be a "t1.tipo_mision= t0.tipo_mision".
SELECT **list of fields***, FROM tipos_misiones_opciones t0, tipos_misiones_opciones_comercio_compra t1 WHERE ((t0.tipo_mision = 'MISION_1') AND ((t1.numero = t0.numero) AND (t0.clase = 'COMERCIO_COMPRA')))
There is no errors, but I get false results because I am getting the values of the first row in the cartesian product.
I have tried to add:
#PrimaryKeyJoinColumns({
#PrimaryKeyJoinColumn(name = "tipo_mision", referencedColumnName = "tipo_mision"),
#PrimaryKeyJoinColumn(name = "numero", referencedColumnName = "numero")
})
But program fails when starting with the following error:
Exception Description: A #PrimaryKeyJoinColumns was found on the annotated element [class TipoOpcionMisionComercioCompra]. When the entity uses a single primary key, only a single (or zero) #PrimaryKeyJoinColumn should be specified.
It seems that for some reason Eclipselink is ignoring tthat the superclass has a composite primary key.
I have a table with the columns vendorid and id (and more, omitted here; using lombok for definition):
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "vendor_store")
public class VendorMeta {
#Id
#Column(name = "vendorid", unique = true)
private String vendorid;
#Column(name = "id", unique = false)
private String id;
}
This is the corresponding repository:
#Repository
public interface VendorMetaRepository extends JpaRepository<VendorMeta, String>, JpaSpecificationExecutor<VendorMeta> {
List<VendorMeta> findByVendorid(String vendorid);
}
I would expect that findByVendorid is returning a single element and findById returns a list, but it's working the opposite way:
Optional vendorMeta = vendorMetaRepository.findById("1");
List vendorMeta2 = vendorMetaRepository.findByVendorid("1");
Both methods return answers for searching vendorid, findbyId is also searching column vendorid.
What do I have to do to get the correct results?
It seems that this is not possible (at least when using lombok). The generated method findById() searches the primary key (whatever the name of this column is) and not the column with the name "id".
It is possible to use any column name for the primary key but not the name "id" for a non-primary-key column.
I'm using QueryHints in Spring Data JPA to use EclipseLink Batch Fetch with a type of IN. Ultimately, I need to use this around 30 fields but it doesn't seem to work right for 2 fields. Field A has a ManyToOne relationship and Field B has a ManyToMany. Based on the results of the initial query, I would expect the batch hint to generate an IN clause with 2 ids for Field A and 12 for Field B. This works fine when the hint is turned on for one field at a time. When it is enabled for both fields, the hint only applies to whichever field is the last hint in the list of QueryHints. I've tried EAGER and LAZY fetch on the fields as a shot in the dark, but it had not impact.
Is there a limitation with mixing batch fetch hints based on the relationship type? Is there something different going on? The EclipseLink documentation isn't very detailed for this feature.
EDIT: It seems it doesn't matter what fields I enable it only, it only works for one at at time. Here is sample code for two entities. The BaseEntity defines the PK id generation.
#Entity
#Table(name = "MainEntity")
public class MainEntity extends BaseEntity implements Cloneable {
...
#ManyToMany(fetch=FetchType.LAZY, cascade=CascadeType.PERSIST)
#JoinTable(
name="EntityBMapping",
joinColumns={#JoinColumn(name="mainId", referencedColumnName="id")},
inverseJoinColumns={#JoinColumn(name="bId", referencedColumnName="id")})
#JsonIgnore
private Set<EntityB> bSet = new HashSet<>();
#ManyToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
#JoinTable(
name="EntityAMapping",
joinColumns={#JoinColumn(name="mainId", referencedColumnName="id")},
inverseJoinColumns={#JoinColumn(name="aId", referencedColumnName="id")})
#JsonIgnore
#OrderColumn(name="order_index", columnDefinition="SMALLINT")
private List<EntityA> aList = new ArrayList<>();
...
}
#Entity
#Cache(type=CacheType.FULL)
#Table(name = "EntityA")
public class EntityA extends BaseEntity {
#Column(name = "name", columnDefinition = "VARCHAR(100)")
private String name;
#ManyToMany(mappedBy = "entityASet", fetch=FetchType.LAZY)
#JsonIgnore
private Set<MainEntity> mainEntityList = new HashSet<>();
}
#Entity
#Cache(type=CacheType.FULL)
#Table(name = "EntityB")
public class EntityB extends BaseEntity {
#Column(name = "name", columnDefinition = "VARCHAR(100)")
private String name;
#ManyToMany(mappedBy = "entityBSet", cascade=CascadeType.ALL)
#JsonIgnore
private Set<MainEntity> mainEntityList = new HashSet<>();
}
The repository query:
#QueryHints(value = {
#QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH_TYPE, value = "IN"),
#QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH_SIZE, value = "250"),
#QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "o.aList")},
#QueryHint(name = org.eclipse.persistence.config.QueryHints.BATCH, value = "o.bSet")},
forCounting = false)
List<MainEntity> findAll(Specification spec);
Generated queries:
SELECT id, STATUS, user_id FROM MainEntity WHERE ((STATUS = ?) OR ((STATUS = ?) AND (user_id = ?)))--bind => [ONESTAT, TWOSTAT, myuser]
..
SELECT t1.id, t1.name, t0.order_index FROM EntityAMapping t0, EntityA t1 WHERE ((t0.mainId = ?) AND (t1.id = t0.aId))--bind => [125e17d2-9327-4c6b-a65d-9d0bd8c040ac]
...
SELECT t1.id, t1.name, t0.mainId FROM EntityBMapping t0, EntityB t1 WHERE ((t1.id = t0.bId) AND (t0.mainId IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)))--bind => [125e17d2-9327-4c6b-a65d-9d0bd8c040ac, 1c07a3a9-7028-48ba-abe8-2296d58ebd57, 235bb4f2-d724-4237-b73b-725db2b9ca9f, 264f64b3-c355-4476-8530-11d2037b1f3c, 2d9a7044-73b3-491d-b5f1-d5b95cbb1fab, 31621c93-2b0b-4162-9e42-32705b7ba712, 39b33b19-c333-4523-a5a7-4ba0108fe9de, 40ba7706-4023-4b7e-9bd5-1641c5ed6498, 52eed760-9eaf-4f6a-a36f-076b3eae9297, 71797f0c-5528-4588-a82c-5e1d4d9c2a66, 89eda2ef-80ff-4f54-9e6a-cf69211dfa61, 930ba300-52fa-481c-a0ae-bd491e7dc631, 96dfadf9-2490-4584-b0d4-26757262266d, ae079d02-b0b5-4b85-8e6f-d3ff663afd6e, b2974160-33e8-4faf-ad06-902a8a0beb04, b86742d8-0368-4dde-8d17-231368796504, caeb79ce-2819-4295-948b-210514376f60, cafe838f-0993-4441-8b99-e012bbd4c5ee, da378482-27f9-40b7-990b-89778adc4a7e, e4d7d6b9-2b8f-40ab-95c1-33c6c98ec2ee, e557acf4-df01-4e66-9d5e-84742c99870d, ef55a83c-2f4c-47b9-99bb-6fa2f5c19a76, ef55a83c-2f4c-47b9-99bb-6fa2f5c19a77]
...
SELECT t1.id, t1.name, t0.order_index FROM EntityAMapping t0, EntityA t1 WHERE ((t0.mainId = ?) AND (t1.id = t0.aId))--bind => [1c07a3a9-7028-48ba-abe8-2296d58ebd57]
As Chris mentioned, Named Queries are the best work around for this issue. The other option is to use a custom repository and call setHint on the EntityManager yourself for each hint specified (plenty of examples out there for creating custom repos in Spring Data JPA). You could attempt to override findOne(...) and protected <S extends T> TypedQuery<S> getQuery(Specification<S> spec, Class<S> domainClass, Sort sort) on SimpleJpaRepository to try and create a generic way to properly set the hints but you'll likely want to check that you don't duplicate hint setting on getQuery(...) as you'll still want to call super() for that and then apply your additional hints before returning the query. I'm not sure what the behavior would be if you applied a duplicate hint. Save yourself the trouble and use Named Queries is my advice.
I've searched for quite a while trying to figure this out. I am using JPA with EclipseLink (Oracle DB). I have a lookup table full of values. I have another table that has a FK relationship to that table. I can insert data fine, but when I try to update the table with a different value, I get an exception. I've tried setting the CASCADE_TYPE but that doesn't have any impact. I thought this would be simple, but maybe I'm missing something.
Lookup table:
public class SomeType implements Serializable {
#Id
#Column(name = "ID")
private Short id;
#Column(name = "TYPE")
private String type;
:
(getters & setters)
}
Contents:
ID Type
------------
1 Type1
2 Type2
3 Type3
: :
Person table (I've left out the Sequencing stuff for brevity):
public class Person implements Serializable {
#Id
#Column(name = "ID")
private Short id;
#JoinColumn(name = "SOME_TYPE", referencedColumnName = "ID")
#ManyToOne(optional = false)
private SomeType someType;
:
(getters & setters)
}
Inserting works fine:
EntityManager em;
:
Person p = new Person();
p.setSomeType(new SomeType(1));
em.persist(p);
That results in:
ID SOME_TYPE
------------------
1 1
But if I want to update Person to change the type:
EntityManager em;
:
Person p = em.find(1);
SomeType newtype = new SomeType(2);
p.setSomeType(newtype);
em.merge(p);
I see the following exception:
Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [SomeType] is mapped to a primary key column in the database. Updates are not allowed.
All I want is the value in the Person table to be updated, like:
UPDATE PERSON set SOME_TYPE = 2 where ID = 1;
Any assistance would be greatly appreciated. Thanks.
Thanks to Chris for answering this:
The update is possible if you use a reference to the managed instance of the object that you want to refer to, not create a new instance.
Person p = em.find(1);
p.setSomeType(em.find(SomeType.class, 2));
em.merge(p);
i have an existing table for TransactionLogs which is either links to a External or to a InternalType. the id's corresponding to the cash adjustment & game transaction are stored in a single column called transaction id and a separate column called type indicates which table is it linked to
Because of the nature of the existing table, i mapped it in a single table inheritance:
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.INTEGER)
public class TransLog implements Serializable {
#Id
#GeneratedValue
private Long id;
private Integer type;
// getters and setters
}
#Entity
public class InternalAdjustmentTransLog extends TransLog {
#ManyToOne
#JoinColumn(name = "TransID", nullable = false)
private InternalAdjustmentRecord internalAdjustmentRecord;
// getters and setters
}
#Entity
public class ExternalTransLog extends TransLog {
#ManyToOne
#JoinColumn(name = "TransID", nullable = false)
private ExternalAdjustmentRecord externalAdjustmentRecord;
}
each of these two subclasses has their subclasses with defined descriminator values..
With the setup given above, there are instances that i need to get a unified data of both
internal and external records. What is the best way to accomplish this? at first i thought it would be enough to use the TransLog as the root class for the query (i'm using jpa criteria). however, i need to get TransId (which are defined in the subclasses and points to 2 different objects of no relationship).
Thanks.
You can make abstract method in TransLog that returns what you need and implement it in both subclasses.