gpt4 book ai didi

java - Hibernate 在数据库中自行创建列

转载 作者:太空宇宙 更新时间:2023-11-04 10:15:20 24 4
gpt4 key购买 nike

我遇到一个问题,hibernate 自己在数据库中创建了两个新列。

该表有一个 orderId 列,hibernate 决定创建另一个列 - order_id。

我已将 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&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;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>

编辑:

根据 23.14. Automatic schema generation of Hibernate 5.2 User Guide :

默认值hibernate.hbm2ddl.autonone如果值为none,则不会执行任何操作.

只需删除hibernate.hbm2ddl.auto的条目从配置文件应该足以实现这一点。

关于java - Hibernate 在数据库中自行创建列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787365/

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