我正在嘗試制作一個簡單的休眠應用程式,當我嘗試使用通常的 java 類運行它時,它運行良好,但是當我嘗試CommandLineRunner使用 Spring Boot 運行它時,它失敗并出現錯誤
java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: org.hibernate.PropertyAccessException: Could not set field value [1] value by reflection : [class springboottest.entity.Item.id] setter of springboottest.entity.Item.id
Caused by: java.lang.IllegalArgumentException: Can not set int field springboottest.entity.Item.id to springboottest.entity.Item
執行時session.save(item1);。
CommandLineRunner效果很好,例如
@Service
public class DBInit implements CommandLineRunner
{
@Override
public void run(String[] args)
{
System.out.println("woring");
}
}
按預期列印“作業”。如果與帶有 的普通類一起
Hibernate使用也可以很好地作業,但不要在.
我在這里不使用任何功能,例如依賴注入或其他東西,只是簡單的代碼。
那么為什么我有一個錯誤以及如何使它作業?javamain()Hibernaterun()CommandLineRunnerSpring Bootjava
// don't working
@Service
public class DBInit implements CommandLineRunner
{
@Override
public void run(String[] args)
{
Configuration configuration = new Configuration().configure();
configuration.addAnnotatedClass(Item.class);
SessionFactory sessionFactory = configuration.buildSessionFactory();
try (Session session = sessionFactory.openSession())
{
Item item1 = new Item("itemName");
session.beginTransaction();
session.save(item1);
session.getTransaction().commit();
}
}
}
// working well
public class DBInit
{
public static void main(String[] args)
{
Configuration configuration = new Configuration().configure();
configuration.addAnnotatedClass(Item.class);
SessionFactory sessionFactory = configuration.buildSessionFactory();
try (Session session = sessionFactory.openSession())
{
Item item1 = new Item("itemName");
session.beginTransaction();
session.save(item1);
session.getTransaction().commit();
}
}
}
物品類別
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
@Entity
@Table(name = "items")
@NoArgsConstructor
public class Item
{
@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
@Getter
@Setter
private int id;
@Getter
@Setter
@Column(nullable = false)
private String name;
public Item(String name)
{
this.name = name;
}
}
休眠.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="http://www.hibernate.org/xsd/orm/cfg">
<session-factory>
<property name="connection.url">jdbc:postgresql://localhost:5432/postgres</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.username">postgres</property>
<property name="connection.password">123</property>
<property name="dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="format_sql">true</property>
<mapping class="springboottest.entity.Item"/>
</session-factory>
</hibernate-configuration>
uj5u.com熱心網友回復:
該代碼混合了“舊式”和彈簧靴。當 DBInit.main 在沒有 spring 的情況下運行時,它會創建SessionFactory,并且當 spring boot CommandLineRunner運行 spring initializeEntityManagerFactory時會提供所有這些sessions。
不同之處在于“sessionFactory”使用它已經擁有的“hibernate.cfg.xml”,<mapping />因此它按預期作業,但EntityManagerFactory不使用hibernate.cfg.xml,因此映射被破壞。檢查此以獲取更多詳細資訊。
如何解決它,對于春天,你應該使用@Repository。博客和這里是解決方案
package springboottest.entity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ItemRepository extends CrudRepository<Item, Integer> {
}
和 DBinit 服務應該如下所示
@Service
public class DBInit implements CommandLineRunner
{
@Autowired
ItemRepository repo;
@Override
public void run(String[] args)
{
System.out.println("CommandLineRunner run() executing...");
repo.save(new Item("ItemName"));
}
}
cfg xml 不是必需的并添加屬性
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format-sql=true
這將使彈簧正常作業。
公關:https ://github.com/Denys-coder/Hibernate_dont_work_in_CommandLineRunner/pull/1
我的回購
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/428679.html
