gpt4 book ai didi

java - Hibernate不将数据保存到H2数据库

转载 作者:行者123 更新时间:2023-12-02 02:29:45 24 4
gpt4 key购买 nike

我正在尝试从 RSS Feed 获取数据并将其放入 H2 表中。我已经有了 RSS 提要,但现在我正在尝试测试是否真的可以将其放入数据库中。

我尝试更改 application.properties 文件或 hibernate.cfg.xml 文件的配置,但无济于事。起初我以为可能是命名约定,但我尝试了一些变体,或者只是向实体类添加注释来查看,但仍然不起作用。

这是 hibernate.cfg.xml 文件:

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:testdbt</property>
<property name="connection.username">sa</property>
<property name="connection.password"/>

<property name="hibernate.default_schema">PUBLIC</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

<property name="hibernate.current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<property name="format_sql">true</property>

<property name="use_sql_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create-drop</property>

<mapping class="com.csdm.rssfeeder.model.RssFeeder"/>

</session-factory>

</hibernate-configuration>

这是实体类:

@Entity
@Table(name = "Feed")
public class RssFeeder {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(name = "TITLE")
private String title;

@Column(name = "DESCRIPTION")
private String description;

@Column(name = "PUBLICATION_DATE")
private String publicationDate;

@Column(name = "IMAGE")
private String image;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getPublicationDate() {
return publicationDate;
}

public void setPublicationDate(String publicationDate) {
this.publicationDate = publicationDate;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

@Override
public String toString() {
return "RssFeeder{" +
"title='" + title + '\'' +
", description='" + description + '\'' +
", publicationDate='" + publicationDate + '\'' +
", image='" + image + '\'' +
'}';
}
}

这是 EntityManager 类:

SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(RssFeeder.class).buildSessionFactory();

Session session = factory.getCurrentSession();

try {
RssFeeder rssFeeder = new RssFeeder();
rssFeeder.setId(1);
rssFeeder.setPublicationDate("fmm");
rssFeeder.setTitle("fmm");
rssFeeder.setImage("fmm");
rssFeeder.setDescription("fmm");
session.beginTransaction();
session.save(rssFeeder);
session.getTransaction().commit();
} finally {
factory.close();
}
}

这是控制台日志。不知道为什么会显示?而不是控制台日志中的实际值。

2019-07-28 00:24:47.139  WARN 8272 --- [           main] org.hibernate.orm.deprecation            : HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
2019-07-28 00:24:47.424 WARN 8272 --- [ main] org.hibernate.orm.connections : HHH10001002: Using Hibernate built-in connection pool (not for production use!)
2019-07-28 00:24:47.427 INFO 8272 --- [ main] org.hibernate.orm.connections : HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:mem:testdbt]
2019-07-28 00:24:47.428 INFO 8272 --- [ main] org.hibernate.orm.connections : HHH10001001: Connection properties: {user=sa, password=****}
2019-07-28 00:24:47.428 INFO 8272 --- [ main] org.hibernate.orm.connections : HHH10001003: Autocommit mode: false
2019-07-28 00:24:47.432 INFO 8272 --- [ main] .c.i.DriverManagerConnectionProviderImpl : HHH000115: Hibernate connection pool size: 1 (min=1)
2019-07-28 00:24:47.436 INFO 8272 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Hibernate:
drop table PUBLIC.Feed if exists
Hibernate:
create table PUBLIC.Feed (
ID integer generated by default as identity,
DESCRIPTION varchar(255),
IMAGE varchar(255),
PUBLICATION_DATE varchar(255),
TITLE varchar(255),
primary key (ID)
)
2019-07-28 00:24:47.471 INFO 8272 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@7793ad58'
Hibernate:
insert
into
PUBLIC.Feed
(ID, DESCRIPTION, IMAGE, PUBLICATION_DATE, TITLE)
values
(null, ?, ?, ?, ?)
2019-07-28 00:24:47.761 INFO 8272 --- [ main] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed drop of schema as part of SessionFactory shut-down'
Hibernate:
drop table PUBLIC.Feed if exists
2019-07-28 00:24:47.770 INFO 8272 --- [ main] org.hibernate.orm.connections : HHH10001008: Cleaning up connection pool [jdbc:h2:mem:testdbt]

最佳答案

我解决了这个问题。我在 hibernate.cfg.xml 文件中出现了拼写错误,指向了错误的数据库。

关于java - Hibernate不将数据保存到H2数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57236521/

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