Hibernate,Spring - 不会在数据库中填充我的表

时间:2017-02-21 23:43:55

标签: java mysql spring hibernate spring-mvc

我现在正在学习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);
}

ProductController的 我尝试将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";
}

任何人都可以帮助我吗? :)

1 个答案:

答案 0 :(得分:0)

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

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

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

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