gpt4 book ai didi

java - Hibernate,Spring - 没有在数据库中填充我的表

转载 作者:行者123 更新时间:2023-11-30 22:03:58 25 4
gpt4 key购买 nike

我现在正在学习 hibernate、spring mvc,我只是想创建一个可以将对象放入数据库的应用程序。但是当我想插入对象时我遇到了问题。我的数据库即使在插入后也是空的,所有字段都是空的,我不知道问题到底出在哪里。

产品类别

@Entity
public class Product {
@Id
private int id;
@Column
private String productId;
@Column
private String name;
@Column
private BigDecimal unitPrice;
@Column
private String description;
@Column
private String manufacturer;
@Column
private String category;
@Column
private long unitsInStock;
@Column
private long unitsInOrder;
@Column
private boolean discontinued;
@Column
private String condition;
public Product(String productId, String name, BigDecimal unitPrice){
this.productId = productId;
this.name = name;
this.unitPrice = unitPrice;
}

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="myDatabase" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>

<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="tutorial" />
<property name="javax.persistence.jdbc.password" value="password" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mojabaza" />

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>

InMemoryProductRepository.class

@Transactional
@Repository
public class InMemoryProductRepository implements ProductRepository {

@PersistenceContext
EntityManager entityManager;

private List<Product> listOfProducts = new ArrayList<Product>();

public InMemoryProductRepository() {
Product iphone = new Product("P1234", "iPhone 5s", new BigDecimal(500));
iphone.setDescription("Apple iPhone 5s, smartfon z 4-calowym wyswietlaczem o rozdielczosci 640x1136 oraz "
+ "8-megapikselowym aparatem");
iphone.setCategory("Smart Phone");
iphone.setManufacturer("Apple");
iphone.setUnitsInStock(1000);
iphone.setId(1);


listOfProducts.add(iphone);

}

public void insertProductToDatabase(Product product) {
entityManager.persist(product);
}

ProductService.class

@Service
public class ProductServiceImpl implements ProductService{

@Autowired
ProductRepository productRepository;

public void insertProductToDatabase(Product product){
productRepository.insertProductToDatabase(product);
}

产品 Controller 我尝试在此处将 2 个对象插入数据库

@Controller
@RequestMapping("/products")
public class ProductController {

@Autowired
private ProductService productService;

@RequestMapping
public String list(Model model){
Product pr = new Product("P1", "okna", new BigDecimal(1000));
pr.setId(1);
productService.insertProductToDatabase(pr);
productService.insertProductToDatabase(productService.getProductsById("P1234"));
model.addAttribute("products", productService.getAllProducts());
return "products";
}

有人能帮帮我吗? :)

最佳答案

尝试添加 entityManager.getTransaction().commit();插入后。

但是,您可以使用更简单的方法通过 spring 数据进行 crud 操作。 例如,

public interface UserRepository extends JpaRepository<User, Long> {  }

然后,您可以简单地将 userrepository 接口(interface)自动连接到某个地方,您可以使用存储库的内置 save()、delete 等方法。

关于java - Hibernate,Spring - 没有在数据库中填充我的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42380095/

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