gpt4 book ai didi

java - Hibernate - 如何使用外键

转载 作者:行者123 更新时间:2023-11-30 07:34:14 24 4
gpt4 key购买 nike

我在外键和 hibernate 方面遇到一些困难。

我正在使用 Spring 4 和 Hibernate 5。我有两个 mysql 表,Utilisateur 和 Adresse。

对我来说,一个用户有一个地址,一个地址有一个用户。所以,这是一对一的关系(如果我犯了错误,请纠正我:))。所以,我这样做了:

@OneToOne
@JoinColumn(name="id")
public Utilisateur getIdUtilisateur() {
return idUtilisateur;
}

但是,当我启动主程序时,出现以下异常:

Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2921)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3421)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
at com.ecollection.dao.AdresseDaoImpl.creerAdresse(AdresseDaoImpl.java:30)
at com.ecollection.service.AdresseServiceImpl.creerAdresse(AdresseServiceImpl.java:26)
at TestMain.main(TestMain.java:39)
Caused by: java.sql.SQLException: Field 'idUtilisateur' doesn't have a default value

这是我的主要:

    UtilisateurServiceImpl utilisateurService = new UtilisateurServiceImpl();

Utilisateur utilisateur = new Utilisateur("Valjeant", "Jean");

utilisateurService.creerUtilisateur(utilisateur);

AdresseServiceImpl adresseService = new AdresseServiceImpl();

Adresse nouvelleAdresse = new Adresse("avenue du champagne", "51100", "reims", "france", utilisateur);

adresseService.creerAdresse(nouvelleAdresse);

你有什么想法吗?

最佳答案

我假设它是双向关系,请尝试:在地址类中:

@OneToOne(fetch = FetchType.LAZY, mappedBy = "adresse", cascade = CascadeType.ALL)
public get getUtilisateur() {
return this.idUtilisateur;
}

在 Utilisateur 类中:

@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
public Adresse getGetAdresse() {
return this.adresse;
}

参见this link

关于java - Hibernate - 如何使用外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35635047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com