Telosys code generation fails when the table has foreign key - java

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

Related

Is it OK to add #Id to an entity which mapped to a table without Primary key column in Spring boot Jpa?

I've started working on a legacy oracle database and using Spring boot Jpa trying to insert a new row in a table (CHANNELMGR_REQUEST) without Identity:
This table has a Numeric column (CM_ISN) and logically could be an Identity candidate, but I could not touch the database for some reason.
I found an existing sequence (CHANNELMGR_SEQ) that is used for generating value for that CM_ISN column too.
So I decided to use that sequence and added some annotation in my equivalent POJO as follow and mapped to the sequence to that CM_ISN column. But not touching the database :
My repository is like this :
While inserting the row, sucessfully invoke the sequence but get exception as bellow :
My questions :
Is it wrong to modify an entity and add #Id to that which is not in in equivalent table?
What's wrong with my code that i get error?
PS: I'm sorry for putting images instead of actual source codes, The reason is because development machine has no access to the internet.
It's wrong if it is not the primary key
Use BigDecimal instead of Number

Pogrammatically get foreign keys for a table in jOOQ

Is there a way to programmatically list all the Fields which are a Foreign Key in jOOQ? It generates a lot of static constants to foreign keys, but there is no good way to programmatically access these.
Example, I have a table Orders with a foreign key field customer_id. In jOOQ, say I have a reference to said table object for Orders, there does not appear to be a way to programmatically get a reference to the customer_id jooq field object. So my only solution now is to manually make these mappings somewhere using a literal map datastructure. Seems like jooq would have been able to do this for me, am I missing something?
There are many ways to navigate the jOOQ meta model. Your description isn't complete, but I'm assuming, you would want to do something like this:
for (ForeignKey<?, ?> fk : ORDERS.getReferencesTo(CUSTOMER))
for (Field<?> fkField : fk.getFields())
System.out.println(fkField);

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

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.

Hibernate Tools 4.0.0 Detect many-to-many tables NOT working with MySQL Db

I am using Hibernate Tools to reverse engineer from a MySQL 5.5.27 using STS 2.9.2 and Hibernate Tools 4.0.0.
It generates the POJO classes correctly, but is NOT picking up on the foreign key relationships defined across the tables (i.e. no sets nor relationships created at all!).
Any idea from your experience whether this tool works with MySQL or what I am doing wrong?
Thanks,
Bob
OK, I got it work at last!
The problem was, as with all things of a technical nature both simple and profound.
In a word: CaseSensitive
I had created my tables with capitaliZatioN (i.e. Person, Phone) and I used the MySQL Workbench to create the foreign keys and they looked like this:
CONSTRAINT FK_Phone_Person FOREIGN KEY (person_id) REFERENCES Person (person_id)
instead of
CONSTRAINT FK_Phone_Person FOREIGN KEY (person_id) REFERENCES person (person_id)
Notice the capitalization.
SOO happy after spending a lot of blood, sweat and tears trying to get that damn thing to work when all along the case was the problem!
many times the default engine of mysql also causes this issue i,e if the default engine through which the tables are create is "MyISAM" then please change it to "InnoDB"; the query to change is ALTER TABLE table_name ENGINE = InnoDB;, hope this works :)
My experience is that the reverse engineering tool needs specific constraints to work, if you have #OneToMany relationships they must have an intermediary table with the ids of both tables and the constraint that you cannot have null values nor orphan rows.
Also the tables will need to be named accordingly to your entities and intermediary tables must be named like owning_entity_entity2 to work.

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).

Categories

Resources