Create an index on a column of a CollectionTable using Hibernate - java

Suppose I have the following entity which models a subscriber and uses a CollectionTable to model a list of subscriptions like so:
#Entity
#Table(name = "SUBSCRIBER")
public class Subscriber {
#ElementCollection
#CollectionTable(name = "PERSON_ORG_SUBSCRIPTIONS",
joinColumns = { #JoinColumn( name = "PERSON_ID", referencedColumnName = "PERSON_ID" ),
#JoinColumn( name = "ORG_ID", referencedColumnName = "ORG_ID" ) } )
#Column(name = "SUBSCRIPTION_NAME")
protected Set<String> _subscriptionNames;
}
So this creates a table with columns for PERSON_ID, ORG_ID and SUBSCRIPTION_NAME.
I'm trying to create a database index on the SUBSCRIPTION_NAME column. But if I put the following annotation on _subscriptionNames:
#org.hibernate.annotations.Index( name="subscription_idx", columnNames={"SUBSCRIPTION_NAMES"} )
I get an exception:
org.hibernate.MappingException: Unable to find logical column name from physical name null in table SUBSCRIBER
I also tried using the org.hibernate.annotations.Table annotation on the Subscriber entity, but there does not seem to be a way to have it reference the PERSON_ORG_SUBSCRIPTIONS table.
I'm using Hibernate 3.5.3 and PostgreSQL 9.0.

Is the column with name "SUBSCRIPTION_NAME" present in the table SUBSCRIBER?
Are you planning to create index on table from the code? You should propably use hibernate.hbm2ddl.auto=create

Related

Primary key not returned in OneToOne join fetch in eclipselink

I have an issue join fetching in case of OneToOne relation in the same class. Example follows:
class Data {
...
#Id
#Column(name = "DATA_ID")
Long id;
#Column(name = "DATA_OWNER_ID")
#ForeignKey(entityClass = Owner.class)
Long ownerId;
#Column(name = "DATA_RELATED_ID")
#ForeignKey(entityClass = Data.class)
Long relatedDataId;
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "DATA_RELATED_ID", referencedColumnName = "DATA_ID", insertable = false, updatable = false)
Data relatedData;
}
I want to select data based on some conditions, while also fetching/initialising the "relatedData", all in one JPQL query:
SELECT owner.something1, data
FROM Data data
JOIN Owner owner on data.ownerId = owner.id
JOIN FETCH data.relatedData
WHERE data.something2 = :expectedSomething2
Executing that JPQL query throws an exception:
Query: ReadObjectQuery(name="relatedData" referenceClass=Data)|Exception:
javax.persistence.PersistenceException: Exception [EclipseLink-6044] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.QueryException
Exception Description: The primary key read from the row [DatabaseRecord(
DATA_X => something
DATA_Y => something2
...
)] during the execution of the query was detected to be null. Primary keys must not contain null.
Which is somewhat true, as there is no DATA_ID column listed. Changing JOIN FETCH to LEFT JOIN FETCH returns both owner.something1 and data, but the relatedData object is null (relatedDataId is not null).
I can see, that the id for relatedData is returned from DB, but eclipselink trims it in valueFromRowInternalWithJoin and trimRowForJoin methods.
The Id column name attribute value is the reason of this exception. Same issue found in eclipselink version 2.3.2 but it works fine in version 2.0.0
Try with this entry :
eclipselink.jpa.uppercase-column-names=true
OR Try with upper and lower case one by one which on will work for you.
#Id
#Column(name = "UUID") // UUID - uppercase/lowercase one by one
Long id;
I've somehow resolved this issue, but haven't had the time to correctly identify the cause. Final (working) version differences are:
I could've forgotten to add get/set for relatedData
I have specified targetEntity = Data.class in #OneToOne
Fetch is now a LEFT JOIN FETCH and appears before JOIN Owner owner

Foreign key constraint fails on many to many Hibernate relationship

I'm currently trying implement a many to many relationship in Hibernate. I followed: https://www.baeldung.com/hibernate-many-to-many however I'm getting an exception that says:
java.sql.SQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`playlist_comparer`.`playlist_tracks`, CONSTRAINT `fk_pt_track_id` FOREIGN KEY (`track_id`) REFERENCES `tracks` (`id`))
Here's the liquibase for my join table:
- createTable:
tableName: playlist_tracks
columns:
- column:
constraints:
primaryKey: true
primaryKeyName: pk_playlist_track_id
name: id
type: bigint
autoIncrement: true
- column:
name: playlist_id
type: VARCHAR(255)
constraints:
foreignKeyName: fk_pt_playlist_id
references: playlists(id)
- column:
name: track_id
type: VARCHAR(255)
constraints:
foreignKeyName: fk_pt_track_id
references: tracks(id)
and the mapping on my Hibernate objects:
Playlist.java:
#ManyToMany(cascade = {CascadeType.ALL})
#JoinTable(name = "playlist_tracks", //
joinColumns = {#JoinColumn(name = "track_id", nullable = false)}, //
inverseJoinColumns = {#JoinColumn(name = "playlist_id", nullable = false)})
private Set<Track> tracks = new HashSet<>();
Track.java:
#ManyToMany(mappedBy = "tracks")
private Set<Playlist> playlists = new HashSet<>();
I save the tracks into the DB first with no association to any playlists in one #Transactional method, leave the transactional boundary and then enter a new #Transactional method where I add the tracks to the Playlist object and then try to save that at which point I get the exception at the top.
Anyone have any ideas?
Turns out I just had the join columns the wrong way round on the join table annotation.

Hibernate: Reference a column in an association, for efficency

For simplicity I'll chop the tables down quite a bit
Given the tables REPORT_DOWNLOAD, REPORT
REPORT has ID, NAME
REPORT_DOWNLOAD has ID, FK_REPORT_ID
I essentially want to build the query:
SELECT R.NAME, RD.ID FROM REPORT_DOWNLOAD RD, REPORT R WHERE
RD.FK_REPORT_ID = R.ID
My entity for REPORT_DOWNLOAD essentially looks like
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "FK_REPORT_ID", referencedColumnName = "ID", nullable = false)
private ReportOrm report;
But that queries the whole REPORT table. Is there a way to reference just the NAME column from the entity?
#SomeAnnotationThatMagicallyPulls("FK_REPORT_ID", "NAME")
private String reportName;
I feel like there must be some Hibernate / JPA magic here that I could use

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity

I am beginner in handling JPA with maven and JBOSS, with Restful to make my application I have the following problem arose me doing DEPLOY
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: com.company.test_resources_war_1.0-SNAPSHOTPU] Unable to build EntityManagerFactory
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: database.Photo column: fid_module (should be mapped with insert = \ "false \" update = \ "false \") "}}
Not that step, check all posles solutions, but did not find anything, can someone help me??
Thanks in advance
Below I show the SQL code in postgres that I have and I did the mapping.
I have three tables (activity, event and photo) where one of them (photo) refers to the other two (activity and event) but in a single column (photo.fid_module)
SQL Code (enginer database-->Postgresql):
CREATE TABLE activity (
id_activity integer not null,
name character varying(150),
description text,
CONSTRAINT id_activity_pk PRIMARY KEY (id_activity)
)
CREATE TABLE event (
id_event integer not null,
name character varying(150),
description text,
date timestamp without time zone,
CONSTRAINT id_event_pk PRIMARY KEY (id_event)
)
CREATE TABLE photo(
id_photo integer not null,
path character varying(150),
fid_module integer not null,
CONSTRAINT id_photo_pk PRIMARY KEY (id_photo),
CONSTRAINT fk_photo_activity FOREIGN KEY (fid_module)
REFERENCE activity (id_activity) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_photo_event FOREIGN KEY (fid_module)
REFERENCE event (id_event) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
Now the mapping I did with the help of Netbenas and gave me the following code (I did the mapping for the three tables, but in presenting me the problem is in the class Photo.java).
#Entity
#Table(name = "photo")
#XmlRootElement
#NamedQueries({
#NamedQuery(name = "photo.findAll", query = "SELECT p FROM Photo p"),
#NamedQuery(name = "photo.findByFidPhoto", query = "SELECT p FROM Photo p WHERE p.fidphoto = :fidphoto"),
#NamedQuery(name = "photo.findByIdPhoto", query = "SELECT p FROM Photo p WHERE p.idphoto = :idphoto")})
public class Photo implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id_photo")
private Integer idPhoto;
#Column(name = "path")
private Recurso fidPath;
#JoinColumn(name = "fid_module", referencedColumnName = "id_activity")
#ManyToOne(optional = false, fetch = FetchType.LAZY)
private SliderWebHome fidModule;
#JoinColumn(name = "fid_module", referencedColumnName = "id_event")
#ManyToOne(optional = false, fetch = FetchType.LAZY)
private Publicacion fidModule1;
public ModuloRecurso() {
}
.......
}
I am using JPA for persistence (but do mvn clean install and mvn jboss-as: deploy several pulls me hibernate dependencies) could anyone tell me what is my mistake or could solve this problem. Thank you.
You have two column mapped with the same name
#JoinColumn(name = "fid_module", referencedColumnName = "id_activity")
#JoinColumn(name = "fid_module", referencedColumnName = "id_event")
Change one of the name attribute!
Looking in your exception, you can read:
Repeated column in mapping for entity
As noted in another answer, your Java code specifies the same join-column name for two fields, which can't work.
If this Java code is generated by a netbeans mapping tool, as it seems from your note
Now the mapping I did with the help of Netbenas and gave me the following code ...
the bad Java mapping is probably caused by a bad combination of constraints in your SQL.
You have in your definition of the photo table:
CONSTRAINT fk_photo_activity FOREIGN KEY (fid_module)
REFERENCE activity (id_activity) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fk_photo_event FOREIGN KEY (fid_module)
REFERENCE event (id_event) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
which attempts to make the column fid_module a foreign key referencing activity and also a foreign key referencing event, which can't work.
If you need foreign keys from photo to both of those tables, you'll need to use two different columns.

JPQL querying a collection of non-entites

I want to make a JPQL query with a collection of non entities. This is my Table entity :
#Entity
#Table(name = "ct_table")
public class Table {
...
#CollectionOfElements(fetch = FetchType.EAGER)
#JoinTable(name = "ct_table_result", joinColumns = #JoinColumn(name = "tableId"))
#MapKey(columns = { #Column(name = "label") })
#Column(name = "value")
private Map<String, String> tableResults;
...
then I try to make a query like this
select count(*) from table where table.tableResults['somekey'].value='somevalue'
but I get the following exception:
Cannot create element join for a collection of non-entities!
Any suggestion??
thanks for your time
EDIT:
I use JPA 1 and hibernate 3.3. Default libraries in JBoss 5
The JPA 2 spec (page 139) defines the KEY() and VALUE() functions to access the key and the value of a map-valued element collection:
select count(t.id) from Table t
where KEY(t.tableResults) = 'somekey' and VALUE(t.tableResults) = 'somevalue'

Categories

Resources