views:

37

answers:

2

Hi,

I have to write an application in Java EE6. I have been given a MySQL database, to which I am not allowed to make any changes. The database is well structured and normalized, but does not have any foreign keys defined to enforce integrity. (It uses the MyISAM engine).

Is it possible to define relationships (using @JoinColumn, @ManyToOne etc.) in JPA Entity classes without defining foreign keys in the database?

+1  A: 

What do you mean by "relationships" in the database? If you just mean foreign key constraints (i.e. you have foreign key values, but no constraints), then you do not need those to define relationships in JPA, just use the foreign key values.

If you just have plane table with nothing relating them at all, then there is not much you can do, you need some data relating the tables. If you can at least create new tables, then you can use JoinTable relationships to relate the tables.

James
I just mean foreign-keys.(Edited by original post to clarify).
Aku
+1  A: 

The database is well structured, but does not have any relationships (foreign keys) defined. (It uses the MyISAM engine).

Foreign keys are used to check and enforce referential integrity, not to join tables. In other words, using MyISAM tables doesn't prevent tables from having relationships between them.

Is it possible to define relationships (using @JoinColumn, @ManyToOne etc.) in JPA Entity classes without actually having them in the database?

As long as such relations exist (independently of having FK to enforce integrity for them), you can use them to map your entities.

Pascal Thivent