Hibernate 自己在数据库中创建列 [英] Hibernate Creating Columns in the Database by itself

查看:42
本文介绍了Hibernate 自己在数据库中创建列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem that hibernate creates two new columns in the database by itself.

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

The table has an orderId column and hibernate decides to create another one - order_id.

我已将 hibernate.cfg 文件中的属性更改为验证"而不是更新",但没有帮助.

I have changed the property in the hibernate.cfg file to "validate" rather than "update" but it didn't help.

这个问题会导致重复键"错误,即使该记录仍然存在于数据库中.

This problem creates a "duplicate key" error, even though the record still goes in the database itself.

这是发生问题的代码 -

Here's the code which in it the problem happens -

<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;
    }

休眠配置文件 =

<?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>

推荐答案

更改为 none

根据 23.14.Hibernate 5.2 用户指南的自动模式生成:

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

Default value of hibernate.hbm2ddl.auto is none and no action will be performed if the value is none.

简单地从配置文件中删除 hibernate.hbm2ddl.auto 的条目就足以实现这一点.

Simply removing the entry of hibernate.hbm2ddl.auto from the config file should suffice to achive that.

这篇关于Hibernate 自己在数据库中创建列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆