当前位置: 首页 > 知识库问答 >
问题:

无法使用Hibernate保存数据

邓丰
2023-03-14
@Entity
public class UserDetails {

    @Id
    private int userId;
    private String userName;
    
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
UserDetails user = new UserDetails();
user.setUserId(1);
user.setUserName("Abhinash");

//creating hibernate session factory
Configuration config = new Configuration();
config.configure();
// local SessionFactory bean created
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
//before saving data in db, we need to open a transaction
session.beginTransaction();
session.save(user);

//once save is done, we need to end the transaction.
session.getTransaction().commit();
Hibernate: insert into UserDetails (userName, userId) values (?, ?)
ERROR: ERROR: relation "userdetails" does not exist
  Position: 13
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1366)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:453)
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3212)
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2380)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:447)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:40)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:281)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101)
    at org.tutorial.hibernate.HibernateTest.main(HibernateTest.java:26)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:103)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:200)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3297)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3824)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:107)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
    at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1(ActionQueue.java:478)
    at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:475)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:345)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:40)
    at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:93)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1362)
    ... 9 more
Caused by: org.postgresql.util.PSQLException: ERROR: relation "userdetails" does not exist
  Position: 13
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.dbcp.initialSize">5</property>
<property name="hibernate.dbcp.maxTotal">20</property>
<property name="hibernate.dbcp.maxIdle">10</property>
<property name="hibernate.dbcp.minIdle">5</property>
<property name="hibernate.dbcp.maxWaitMillis">-1</property>
<mapping class="org.tutorial.hibernate.model.UserDetails" />

提前谢谢!!

共有1个答案

东郭勇
2023-03-14

关键信息是:

错误:关系“UserDetails”不存在

这表明您的Hibernate配置尚未自动创建表。您需要手动创建表,或者添加以下hibernate属性:

 hibernate.hbm2ddl.auto=update
 类似资料:
  • 问题内容: 环境 hibernate4.2 ojdbc6-Oracle 11.2.0.3.0 JDBC 4.0 Oracle数据库11g 问题 我们遵循了许多建议,以如下方式配置Hibernate批处理: 我们检查了日志,发现生成的SQL语句已批处理。但是,如果两个事务同时修改相同版本的实体行,则Hibernate将成功提交它们,导致最后提交的事务中的冲突更新丢失(两个事务中都保存了无冲突的数据,

  • 07-24 12:36:23.742: W/System.err(10386):java.io.IO异常:拒绝许可07-24 12:36:23.750: W/System.err(10386): atjava.io.File.createNewFileImpl(本地方法)07-24 12:36:23.750: W/System.err(10386): atjava.io.File.createNe

  • 我不是JPA/Hibernate方面的专家,我真的不知道我试图实现的是不可能实现的还是我做错了,既然我预计是后者,这里就什么也不做了。 我有一个map ,我正试图将其持久化到一个表中,并遵循了这里描述的示例:使用JPA存储map 但随后我得到一个HibernateException:列值的STATE_MAP中的列类型错误。找到:bigint,应为:char(255) 当我将bigint更改为cha

  • 问题内容: 我正在尝试使用hibernate将数据插入数据库。这是我要执行的动作 但这给我一个错误的说法 线程“主”中的异常org.hibernate.TransientPropertyValueException:非null属性引用了一个瞬态值- 必须在当前操作之前保存瞬态实例 这是我的学生详细信息实体 我的学生详细信息hbm.xml 我的主题实体看起来像 Subject.hbm.xml 这是S

  • 这是使用电子邮件密码注册帐户的所有代码,保存验证电子邮件,将用户数据保存到Firestore数据库中。只有Firestore数据库无法运行。 else { Toast.makeText(register.this, “Error ! ” task.getException().getMessage(), Toast.LENGTH_SHORT).show();progressBar.setVisib