Hi,
I have a problem with Hibernate to write Foreign Keys to the database. Here is the following code:
Tables:
Code:
-- -----------------------------------------------------
-- Table `tbl_Benutzer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `tbl_Benutzer` (
`idBenutzer` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`BenutzerName` VARCHAR(45) NOT NULL ,
`BenutzerMail` VARCHAR(75) NOT NULL ,
`BenutzerPasswort` VARCHAR(45) NOT NULL ,
`FK_Sprache` BIGINT UNSIGNED NOT NULL ,
`FK_Land` BIGINT UNSIGNED NOT NULL ,
PRIMARY KEY (`idBenutzer`) ,
INDEX `fk_tbl_Benutzer_tbl_Sprache1_idx` (`FK_Sprache` ASC) ,
INDEX `fk_tbl_Benutzer_tbl_Land1_idx` (`FK_Land` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `tbl_Land`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `tbl_Land` (
`idLand` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`LandNameDeutsch` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idLand`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `tbl_Sprache`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `tbl_Sprache` (
`idSprache` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`SpracheNameDeutsch` VARCHAR(45) NOT NULL ,
`SpracheIcon` BLOB NULL ,
`SpracheNameAnzeige` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idSprache`) )
ENGINE = InnoDB;
Here the Entities:
Code:
@Entity
@Table(name = "tbl_Sprache", catalog = "db_xxx", schema = "")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "VOSprache.findAll", query = "SELECT t FROM VOSprache t"),
@NamedQuery(name = "VOSprache.findByIdSprache", query = "SELECT t FROM VOSprache t WHERE t.idSprache = :idSprache"),
@NamedQuery(name = "VOSprache.findBySpracheNameDeutsch", query = "SELECT t FROM VOSprache t WHERE t.spracheNameDeutsch = :spracheNameDeutsch"),
@NamedQuery(name = "VOSprache.findBySpracheIcon", query = "SELECT t FROM VOSprache t WHERE t.spracheIcon = :spracheIcon"),
@NamedQuery(name = "VOSprache.findBySpracheNameAnzeige", query = "SELECT t FROM VOSprache t WHERE t.spracheNameAnzeige = :spracheNameAnzeige")
})
public class VOSprache implements Serializable {
// --------------------------- Variablen -----------------------------------
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idSprache", nullable = false)
private Integer idSprache;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "SpracheNameDeutsch", nullable = false, length = 45)
private String spracheNameDeutsch;
@Size(max = 45)
@Column(name = "SpracheIcon", length = 45)
private byte[] spracheIcon;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "SpracheNameAnzeige", nullable = false, length = 45)
private String spracheNameAnzeige;
// .........................................................................
...
}
@Entity
@Table(name = "tbl_Land", catalog = "db_xxx", schema = "")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "VOLand.findAll", query = "SELECT t FROM VOLand t"),
@NamedQuery(name = "VOLand.findByIdLand", query = "SELECT t FROM VOLand t WHERE t.idLand = :idLand"),
@NamedQuery(name = "VOLand.findByLandNameDeutsch", query = "SELECT t FROM VOLand t WHERE t.landNameDeutsch = :landNameDeutsch")
})
public class VOLand implements Serializable {
// --------------------------- Variablen -----------------------------------
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idLand", nullable = false)
private Integer idLand;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "LandNameDeutsch", nullable = false, length = 45)
private String landNameDeutsch;
// .........................................................................
.....
}
@Entity
@Table(name = "tbl_Benutzer", catalog = "db_xxx", schema = "")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "VOBenutzer.findAll", query = "SELECT t FROM VOBenutzer t"),
@NamedQuery(name = "VOBenutzer.findByIdBenutzer", query = "SELECT t FROM VOBenutzer t WHERE t.idBenutzer = :idBenutzer"),
@NamedQuery(name = "VOBenutzer.findByBenutzerName", query = "SELECT t FROM VOBenutzer t WHERE t.benutzerName = :benutzerName"),
@NamedQuery(name = "VOBenutzer.findByBenutzerMail", query = "SELECT t FROM VOBenutzer t WHERE t.benutzerMail = :benutzerMail"),
@NamedQuery(name = "VOBenutzer.findByBenutzerPasswort", query = "SELECT t FROM VOBenutzer t WHERE t.benutzerPasswort = :benutzerPasswort"),
@NamedQuery(name = "VOBenutzer.findByFKSprache", query = "SELECT t FROM VOBenutzer t WHERE t.fkSprache = :fkSprache"),
@NamedQuery(name = "VOBenutzer.findByFKLand", query = "SELECT t FROM VOBenutzer t WHERE t.fKLand = :fKLand")
})
public class VOBenutzer implements Serializable {
// --------------------------- Variablen -----------------------------------
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idBenutzer", nullable = false)
private Integer idBenutzer;
@Basic(optional = false)
@NotNull
@Size(min = 3, max = 45)
@Column(name = "BenutzerName", nullable = false, length = 45)
private String benutzerName;
@Basic(optional = false)
@NotNull
@Size(min = 3, max = 75)
@Column(name = "BenutzerMail", nullable = false, length = 75)
@Email
private String benutzerMail;
@Basic(optional = false)
@NotNull
@Size(min = 3, max = 45)
@Column(name = "BenutzerPasswort", nullable = false, length = 45)
private String benutzerPasswort; // Nur der Hash des Passworts wird gespeichert
@Basic(optional = false)
//@NotNull
//@Column(name = "FK_Sprache", nullable = false)
@Column(name = "FK_Sprache")
private VOSprache fkSprache;
@Basic(optional = false)
@NotNull
@ManyToOne
@JoinColumn(name = "FK_Land")
private VOLand fKLand;
// .........................................................................
....
}
Here the code to create the data:
Code:
// erzeugen der Sprache
DAOSprache dao = new DAOSprache();
dao.setEntityManager(this.em);
VOSprache sprache = new VOSprache();
sprache.setSpracheIcon(null);
sprache.setSpracheNameAnzeige("Deutsch3");
sprache.setSpracheNameDeutsch("Deutsch4");
sprache = dao.create(sprache);
System.out.println("sprache = " + sprache.getIdSprache());
// erzeugen des Landes
DAOLand dao1 = new DAOLand();
dao1.setEntityManager(this.em);
VOLand land = new VOLand();
land.setLandNameDeutsch("Deutschland");
land = dao1.create(land);
System.out.println("land = " + land.getIdLand());
// erzeuge Benutzer 1
DAOBenutzer dao3 = new DAOBenutzer();
dao3.setEntityManager(em);
VOBenutzer benutz1 = new VOBenutzer();
benutz1.setBenutzerMail("adsdsada@gmx.de");
benutz1.setBenutzerName("Nutzername");
benutz1.setBenutzerPasswort("asdasd");
benutz1.setFKSprache(sprache);
benutz1.setFKLand(land);
dao3.create(benutz1);
Exception:
Code:
org.hibernate.exception.DataException: could not execute statement
javax.persistence.PersistenceException
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1152)
at de.xxx.db.DAO.common.ACDao.create(ACDao.java:84)
at de.xxx.db.DAO.DAOxxxTest.erzeugexxxTest(DAOxxxTest.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: org.hibernate.exception.DataException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:52)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:84)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2791)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3361)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:474)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:179)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:163)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:198)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:109)
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:774)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:747)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:752)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
... 34 more
Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect integer value: '\xAC\xED\x00\x05sr\x00!de.xxx.db.entities.VOSprache\x00\x00\x00\x00\x00\x00\x00\x01\x02\x00\x04L\x00\x09idSprachet\x00\x13L' for column 'FK_Sprache' at row 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3489)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 55 more
The create Method of the DAO does:
Code:
public T create(T t) {
this.em.persist(t);
this.em.flush();
this.em.refresh(t);
return t;
}
Has anybody an idea why this error happens, when hibernate trys to save the entity with the foreign key!?
If change it in the database and at the entities to null, then hibernate saves the data!!
Regards nathaniell