0

I'm using JPA for the first time and still got some problems with it. Meanwhile I get connected to my database and JPA is creating not already existing Tables automatically.

My problem is that the created tables wont define table columns as Foreign Key which I marked as such.

Here is how I declare a Foreign Key in my program:

@ManyToOne(optional = false)
@JoinColumn(nullable = false, name = "ATT_ID")
private Attribute att;

As database I use MySQL community server 5.7.19 which shows me this as DDL for the newly created Table

CREATE TABLE `TABLE` (
  `ID` int(11) NOT NULL,
  `FOREIGN_ID` int(11) NOT NULL,
  `FOREIGN_ID2` int(11) NOT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `STUFF` (`ID`),
  KEY `STUFF` (`FOREIGN_ID`),
  KEY `STUFF` (`FOREIGN_ID2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

I changed some things for security reasons, but as you can see, the Columns are clearly not marked as foreign keys.

I don't know what I am doing wrong, I searched the web for explanations, but every thread etc. I red just said to use the Annotations I am already using.

Does anyone know what I am doing wrong?

Thanks in advance

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Demli95
  • 136
  • 1
  • 12
  • Is the Attribute table an InnoDB table, too? Have you tried dropping the table and have Hibernate recreate it? Are you using the `org.hibernate.dialect.MySQLInnoDBDialect'? – fhossfel Oct 03 '17 at 14:15
  • Sorry probably should have mentioned that right away Im using Apache OpenJPA instead of hibernate. Yeah I have tried to drop it and letting the table recreate by JPA but that didnt work out. Im not quite sure what an InnoDB table is, is that important? Ive just glanced through the wikipedia article there stood it is set up defaulty in MySQL versions later than version 5.5 so i guess the table is InnoDB? How do I check that?/How do I tell OpenJPA to create it as InnoDB? – Demli95 Oct 03 '17 at 17:11
  • Looks like further config needed. See the answers here: https://stackoverflow.com/questions/10677950/ddl-generation-and-general-persistence-xml-settings-openjpa – Alan Hay Oct 03 '17 at 17:46

0 Answers0