我有一个问题,hibernate本身在数据库中创建了两个新列。
该表具有一orderId列,并且hibernate决定创建另一个-order_id。
orderId
我已将hibernate.cfg文件中的属性更改为“验证”而不是“更新”,但没有帮助。
hibernate.cfg
即使记录仍在数据库本身中,此问题也会导致“重复键”错误。
这是发生问题的代码-
<body> <h1>Your cart</h1> <form action="/order_placed" method="get"> <table id="table"> <tr> <th>Product ID</th> <th>Name</th> <th>Brand</th> <th>Rating</th> <th>Price</th> <th>quantity</th> <tr th:each="product : ${checkout}"> <td th:text="${product.productId}"></td> <td th:text="${product.productName}"></td> <td th:text="${product.brand}"></td> <td th:text="${product.rating}"></td> <td th:text="${product.price}"></td> <td><input type="text" placeholder="insert quantity" id="quantity" required></td></tr> </table> <br><br> <input type="text" id="notes" name="customernotes" placeholder="Enter notes here..."> <br><br> <span id="sum" style="font-size: larger"></span> <script> var table = document.getElementById("table"), sumVal = 0; for (var i = 1; i < table.rows.length; i++) { sumVal += parseInt(table.rows[i].cells[4].innerHTML); } document.getElementById("sum").innerHTML = "Total payment sum =" + sumVal; </script> <br><br> <button class="button button1" id="btn" type="submit" onclick="myfunction()">Place your order</button> <br><br> <script> function myfunction() { var tsum = document.getElementById("sum").innerHTML.split("=")[1]; var cnotes = document.getElementById("notes").value; var xhttp = new XMLHttpRequest(); xhttp.open("GET","/order_placed" + '?cusnotes=' + cnotes + '&totalsum=' + tsum,true); xhttp.send(); } </script> </form>
映射器-
@RequestMapping("/checkout") public ModelAndView checkout(@CookieValue("clientidcookie") String clientid){ ModelAndView mav = new ModelAndView("checkout"); Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Session session_for_db = factory.openSession(); ArrayList<CartEntity> carts = (ArrayList<CartEntity>)session.createQuery("from CartEntity ").list(); ArrayList<ProductsEntity> products = (ArrayList<ProductsEntity>)session_for_db.createQuery("from ProductsEntity ").list(); ArrayList<ProductsEntity> res = new ArrayList<ProductsEntity>(); for(CartEntity c: carts){ if (c.getClientId().equals(clientid)){ for(ProductsEntity p: products){ if (p.getProductId().equals(c.getProductId())){ res.add(p); } } } } mav.addObject("checkout",res); session.close(); factory.close(); return mav; }
hibernate配置文件=
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url">jdbc:mysql://localhost:3306/theprocess?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC</property> <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">Amit4089</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- DB schema will be updated if needed --> <property name="hbm2ddl.auto">update</property> <mapping class="com.example.WebAppProcess20.Entities.ClientsEntity"/> <mapping class="com.example.WebAppProcess20.Entities.InvoicesEntity"/> <mapping class="com.example.WebAppProcess20.Entities.OrdersEntity"/> <mapping class="com.example.WebAppProcess20.Entities.OrdersitemsEntity"/> <mapping class="com.example.WebAppProcess20.Entities.ProductsEntity"/> <mapping class="com.example.WebAppProcess20.Entities.CartEntity"/> </session-factory> </hibernate-configuration>
改成<property name="hbm2ddl.auto">none</property>
<property name="hbm2ddl.auto">none</property>
编辑:
根据 23.14. Automatic schema generation 《 Hibernate 5.2用户指南》 :
23.14. Automatic schema generation
默认值为hibernate.hbm2ddl.autois none,如果值为,则不执行任何操作none。
hibernate.hbm2ddl.auto
none
只需hibernate.hbm2ddl.auto从配置文件中删除条目即可满足要求。