Resolves the Hibernate4 execution of Save () or update () invalid problem _java

Source: Internet
Author: User

Recently when I was writing an online mall project, I encountered a problem: Hibernate after executing the Save () or update () method, there is no effect, there is no change in the database, and the console has not reported any errors, which makes me very silent ...
I looked it up on the internet, and some people said it was the primary Key's self growth problem, some people say that is not open the transaction, so unable to write or update the database, I looked at their analysis, said all reasonable, but these solutions do not work for me, because my primary key is not a problem, the transaction is managed by spring , all other save operations are OK, no problem.
Since there are no problems objectively, so I focus on the specific to save or update the object, the Pojo did a careful analysis, first look at the object I want to save the corresponding database table:

/*=============================*/ 
/* Table: Merchandise LIST Structure      * * 
/*=============================*/ 
Create Table Product 
(/// 
  * Commodity number, auto growth */ 
  ID         int PRIMARY key NOT NULL auto_increment,// 
  * Commodity name 
  /* NAME
   varchar (+),/ 
  * Commodity prices        /Price Decimal (8,2),/ 
  * Picture/ 
  pic         varchar (+),/ 
  * Brief introduction 
  to the product/remark       Longtext,/ 
  * Product Details/ 
  xremark       longtext,/ 
  * Product Production date * 
  *        timestamp default current_timestamp,// 
  Whether it is recommended goods, the recommended products can be displayed in the mall home * * * 
  commend       bool, 
  * Whether it is a valid product, the effective goods can be displayed in the mall home * * * * *        , 
  the category number of the product/ 
  cid         int, 
  constraint CID_FK FOREIGN KEY (CID) references category (ID) 

Then the specific pojo is not posted, which is based on some of the field properties generated by this table and the set and get methods. I think the most likely problem field is this time date, so I looked at the following code for date in Pojo:

@Entity public 
class Product implements java.io.Serializable { 
 
  //Fields 
  private Timestamp date; 
 
  Omit other extraneous code 
 
  ... @Column (name = "Date", Nullable = false, length =) public 
  Timestamp getDate () {return 
    this.date; 
  } 
 
  public void setdate (Timestamp date) { 
    this.date = date; 
  } 
} 

So I once again on the Internet search this timestamp, found that the problem is in this, will timestamp to Java.util.Date can be changed. A Date object is then passed in, and hibernate is automatically converted to the timestamp type.
This question also gives me a revelation : The inability to perform database operations may also be an issue with the object itself, from between the table's fields and the Pojo property.

Original link: http://blog.csdn.net/eson_15/article/details/51383298

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.