I have two tables with the same name in different schemas and I am trying to create an entity using both these tables and #SecondaryTable. It gives me the following error
InFlightMetadataCollector$DuplicateSecondaryTableException: Table with that name [EMPLOYEE] already associated with entity
Here is my code
#Entity
#Table(name = "EMPLOYEE", schema = "S1", catalog = "")
#SecondaryTable(name = "EMPLOYEE", schema = "S2", catalog = "", pkJoinColumns = {#PrimaryKeyJoinColumn(name = "EID", referencedColumnName = "ENO")})
public class Employee {
It seems this is a bug in Hibernate, Hibernate ORM team says on the ticket HHH-12423 that this is fixed and will be available from hibernate version 5.3.0
name needs to be unique in order to be referenced inside the column annotation ... I have two ideas which I didn't try .... you can make an alias to one database table if you have privelege to do and then refer to this alias (i don't know if it works or no) .... or you can define the table name as "s1.employee" and "s2.employee" , some providers allow it but i am not sure if hibernate does or no ... if any of these worked with u please let us know
Related
There is a one-to-many relationship in my model, where the child entity is stored in two tables.
#Entity
#Table(name = "child1")
#SecondaryTable(name = "child2", pkJoinColumns = {
#PrimaryKeyJoinColumn(name = "id1", referencedColumnName = "id1"),
#PrimaryKeyJoinColumn(name = "id2", referencedColumnName = "id2")})
#Where(clause = "col1 is not null and col2 is not null")
#Data
#Immutable
public class Child implements Serializable {...}
Child entity is fetched eagerly together with Parent entity. Problem lies within #Where clause, which should reference columns from two tables: col1 is in table child1 and col2 is in child2. This throws the following error:
ERROR 12333 --- [nio-8183-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper : Column (col2) not found in any table in the query (or SLV is undefined).
java.sql.SQLException: null
...
Using only: #Where(clause = "col1 is not null") gives propper mapping and results in no error.
Using #Where(clause = "child1.col1 is not null and child2.col2 is not null") gives the following error:
Column (child1) not found in any table in the query (or SLV is undefined).
How can I make #Where work with two tables or is there any workaround?
There are some requirements though:
I'm using informix as an underlying database and have read-only access.
I know, that it can be solved by native SQL or even JPQL / criteria API and so on, but doing so would make me rewrite a lot of core. I want to avoid it.
This is due to the HHH-4246 issue.
A workaround would be to replace the #SecondaryTable with a #OneToOne association using #MapsId.
This way, the child2 table becomes the Child2 entity for which you can use #Where or #Filter.
In my application there is an entity:
#Entity
#Table(schema = "hr", name = "personal_data")
public class PersonalData {
}
and connection string defined in Spring's application.properties:
spring.datasource.url=jdbc:mysql://localhost/mobile?UseUnicode=true&characterEncoding=utf8
If I invoke the following code:
TypedQuery<E> typedQuery = em.createQuery("from PersonalData pd where pd.employeeId = ?1", PersonalData.class);
typedQuery.setParameter(1, 123);
return typedQuery.getSingleResult();
it will result in this SQL:
select * from personal_data personalda0_ where personalda0_.employee_id=?
Which will fail with the exception
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mobile.personal_data' doesn't exist
because the table personal_data is defined in the hr database and there is no such table in mobile.
This was working fine(i.e. table name in SQL was prefixed with database name) in Hibernate 4.3.13 and stopped when the application was migrated to Spring Boot 2.0 which uses Hibernate 5.2.14. Is there any way to achieve the old behaviour in Hibernate 5.x?
I can say that there is a misunderstanding between Hibernate 5 and MySQL, a long story here Hibernate 5.0.6 Ignores schema in MySQL
One Solution is proposed is to use the name of schema in the place of catalog so instead of :
#Table(schema = "hr", name = "personal_data")
^^^^^^
You can use :
#Table(catalog = "hr", name = "personal_data")
^^^^^^^
Also take a look at this :
5.0 Migration Guide
I want to put all of the following autogenerated tables into a specific schema.
#Entity
#Table(name = "master_table", schema = "test")
public class MasterTable {
#OneToMany
private List<VideoEntity> videos;
#Entity
#Table(name = "video_entity", schema = "test")
public static class VideoEntity {
}
}
Result: there are the two entity tables in test schema, but also one in the public schema called master_table_videos for the list mapping.
Question: how can I tell hibernate to also put the list-mapping table in the same schema than the others?
I think you should use the #JoinTable annotation, at least that allows to set the schema name in standard JPA. Check the JavaDoc for Java EE 7 or Java EE 6.
So it would be something like #JoinTable(name = "master_to_videos", schema = "test" ), and you could also specify the name of the join column if required.
Hibernate will create the table in whichever persistence.xml the entity is defined in. So if MasterTable and VideoEntity are both in persistence.xml, it will create both tables in the configured data schema.
I agree with Hein Blöd i tested the #joinTable annotation after any other annotation like #ManyToOne #OneToMany ... as for your example it becomes like this
#OneToMany
#JoinTable(schema = "testSchema" )
private List<VideoEntity> videos;
testSchema is interpreted by Hibernate as test-schema
i know this is for an old question i'm writing this so that any one
right now can find the correct answer i search the internet and this is the first question i found.
I just enabled the JPA validation in eclipse and in shows me some errors (The code is actually running fine).
I have an article entity and it holds a refrence to the next article and the previous entity (of the same type). The validator complains with the message:
Column "nextArticle" cannot be resolved on table "article"
What does this mean exactly? The SQL table has the columns as well. I tried also to map the variables to each other with the "mappedBy" and "JoinColumn" annotation, but was not able to resolve the validation error.
That's the class and validation error:
And that' the mapping:
Edit: Tried the suggestion from anttix: The columns in the table are named "nextArticle_id" and "prevArticle_id", so I came up with that code:
#OneToOne(mappedBy = "prevArticle")
#JoinColumn(name = "nextArticle_id")
public Article getNextArticle() {
return nextArticle;
}
#OneToOne(mappedBy = "nextArticle")
#JoinColumn(name = "prevArticle_id")
public Article getPrevArticle() {
return prevArticle;
}
But the validator complains now about the "mappedBy" annotation with the message:
In attribute 'prevArticle', the "mapped by" attribute 'nextArticle' has an invalid mapping type for this relationship.
Edit 2: I found the solution. I had to tell the validator the names of the columns in the actual database with the #Column annotation like this:
Eclipse can't find a column called prevArticle in the table. You should specifiy the column name for nextArticle and create a bidirectional relation with prevArticle to indicate that it does not need a foreign key column of its own.
#OneToOne
#JoinColumn(name = "next_id")
private Article nextArticle;
#OneToOne(mappedBy = "nextArticle")
private Article prevArticle;
You can omit the #JoinColumn from nextArticle if you want, but I would keep it there to make it clear which relation "owns" the foreign key column.
See also:
http://en.wikibooks.org/wiki/Java_Persistence/OneToOne
I'm currently struggling with mapping an existing db schema to jpa entities, and among a lot of weirdness, I've become stuck on this problem.
I have two tables, similar to this:
Table 1 Table 2
|Service | |Servicetype |
|servicetype | |Servicecategory |
| | | |
Where servicetype in table 1 is a foreign key to service type in table two.
However, the services in table 1 have wildly different behaviour based on which category they belong to (while there are 100+ servicetypes, there are only 4 categories)
I'd like to be able to be a able to map table 1 to four different entityclasses, based on the category of their service type.
This is what I have so far:
#Entity
#Table(name = "table1")
#DiscriminatorColumn(name = "servicecategory", discriminatorType =
discriminatorType.INTEGER)
#DiscriminatorValue("1")
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#SecondaryTable(name = "table2",
pkJoinColumns =
#PrimaryKeyJoinColumn(name = "servicetype", referencedColumnName =
"servicetype"))
public class AbstractService implements Serializable {
...etc
And 4 classes extending from this, buth with a different discriminatorvalue, Which doesn't work, since eclipselink, which I'm using, tries to lookup the value of servicecategory in table1.
Is it possible to express such a mapping with jpa or should I just do the mapping with "where servicecategory = ?" on each query.
Please file an enhancement in EclipseLink to add support for this feature. A JPA solution would be to switch the primary table with the secondary table - this might cause problems with insertion order though and foreign keys will be referencing the Servicetype in table2.
An EclipseLink specific solution is to use a customizer to change the table used for the descriminator field. Add the #Customizer tag to the entity and specify a class with a method:
public void customize(ClassDescriptor descriptor) {
descriptor.getInheritancePolicy().getClassIndicatorField().setTable(
descriptor.getTable("table2"));
}