It is possible to maintain relation between two table in Hibernate level? - java

I am new to Hibernate. Can you please answer my question?
Is it acceptable to maintain the 2 table relations in ORM level? I dont want to add foreign key relation between two tables, but I would like to add many to one relation in hbm file.
Ex I have Account and Account_Type tables. Account table contain AcntType column. Its not null column. AccountType contain AcntType as PK. There is no FK relation in Account table.
Now I would like to add relation from ORM level, I don't want to alter Account table and add foreign key constraint now.
I would like to add "many-to-one" attribute in my Account hbm file. I would like to add "one-to-many" attribute in my AccountType hbm file.
Is it acceptable to maintain relation in ORM level nor from DB level. I am using Oracle DB

Hibernate doesn't care if there is a foreign key constraint or not between the tables.
But adding a foreign key constraint guaranteees that, whatever you use to update your database, and whatever bug your code could have, you'll never have an account referencing an account type that does not exist. So you should definitely have a foreign key constraint.

Related

JPA mapping in 2 entities to model 2 tables having reciprocally a foreign key each other

I have to create the JPA entities from an existing Oracle database that i am not allowed to change the structure.
In this existing Oracle database that i can not change, i have 2 tables :
a table A that presents a foreign key to the table B
a table B that presents a foreign key to the table A
There is NO assocation table between tables A and B.
I have created an entity corresponding to the table A and another entity class corresponding to the table B.
But i can not find the good relationship between these 2 entities.
#OneToMany in these 2 entities is not good.
But #ManyToMany does not convince me too.
Please, have you an idea of a correct mapping for this strange relationship between 2 tables ?
Thank you for your answer.

Telosys code generation fails when the table has foreign key

I have a issue when generating code using Telosys.
After configuring all the information, when I click on Create model
I get the following issue:
If I remove foreign key from table and click Create model then this works fine.
It has become like have to remove the foreign key from table and Create model and then add back the foreign key again.
How to do this without removing foreign key constraint?
PS: I just use this tool to create Spring JPA entity.
After looking in the source code it seems that the table name referenced by the FK is not found in the model.
May be due to upper case / lower case difference in the table name. For example a FK referencing the "Foo" table instead of "FOO" (or vice versa).
You can check the table names retrieved from the database with "Get tables" in the "Meta-data" tab. Use "Get foreign keys" to check the Foreign Keys :
I guess you skipped configuration. You can customize existing templates in order to generate jpa DAO
the templates available on GitHub : https://github.com/telosys-templates-v3

Hibernate Entity with 2 foreign key options

I have that use case:
I have table A and table B (none of them is extending the other, they are logically different). Both tables should have a link with one or more 'documents'.
So I can design table A and table B, but how to design the table for the 'documents'? Should it have 2 foreign key constraints (there is a possibilite one record from documents to have only 1 foreign key to Table A or B and the other foreign key will be null)? How I will declare that in SQL and in Hibernate/JPA. Thanks in advance.
You can have one-to-many unidirectional relationship from Table A/B to documents. By doing so, you will require link tables between Table A/B and document (in other words, document will be a master table). Also you will require to store document separately first and then can assign the documents to the Table A/B entities.
Alternatively, if want to save the documents along with Table A/B entity, then you need to maintain both foreign keys which will be nullable i.e. either A or B.
Both approaches are fine hence depending on your business requirement you should pick one.

JPA: InvalidStateException Error + Composite Key/EmbeddableId/Sequence Generated IDs

I currently have a schema set up with my database and Java Application using OpenJPA that works most of the time, but sometimes I get the error for a few users:
org.apache.openjpa.persistence.InvalidStateException: Attempt to set column "table1.ID" to two different values
table1 actually has a composite Key (two values) and each value in that key is a foreign key to another table. I used RSA (Rational Software Architect) to set up the entities for me (generated code). It set up a PK class (using #EmbeddableId to reference the PK class) in the Entity class for table1, and then two #ManyToOne relationships in the same table1 Entity class (and also in the entity classes that those columns reference) since they are foreign keys
Now, as I mentioned above, each value in the composite key is a foreign key. Well, each of those foreign keys is actually generated using an outside Sequencer in their own entity classes. I am using DB2 and using #GeneratedValue on the columns (i.e. the IDs in table2's and table3's entity classes). I use strategy=GenerationType.SEQUENCE also for each.
Again, everything works USUALLY but not 100% of the time and I'm unsure why. I have gotten rid of this error by wiping out everything and resetting the Sequence Generators, but I know this is definitely not a solution. Could it have something to do with the fact that the two Composite Key values in the database are foreign keys to columns which were generated using a sequence, but the PK entity might not know?
I have noticed too that it only works for users who have a record in the Users table (one of the foreign keys mentioned above is to a Users table, while the other FK is to another table). What happens, if a user is not in the table, it creates one, something like:
User newUser = userManager.getNewUser();
newUser.setName(..);
newUser.setEmail(..);
...
When it's done, the PK class I mentioned above has a new instance of that created, which is then called into another table. The ID from the user above is passed into the PK. Like:
PK newPK = pkManager.getNewPK();
newPk.setAID(newUser.getID());
Has anybody run into this? Any solutions?
Sorry, fixed the problem. I went through my code and realized I had forgot to refactor one line of code (change in data model).

Relational Database (H2, Java): How do I constrain a foreign key to NOT match another foreign key in the same table?

Simple question. Just wondering if this can be done without me having to enforce this constraint manually in my Java code. These two foreign keys (together in the same table) both refer out to another table, but for each row, they must not be allowed to point to the same foreign item.
link text
You can use a check contraint to enforce that two columns have different values:
ALTER TABLE TableName
ADD CONSTRAINT ConstraintName
CHECK fk1 <> fk2

Categories

Resources