Recently, when writing online mall projects, I encountered a problem: Hibernate did not have any effect after executing the save() or update() method, there was no change in the database, and the console did not report any errors, which made me speechless...
I checked online and some people said it was a self-growth problem of primary keys, and some people said that the transaction was not started, so it was impossible to write or update the database. I read their analysis in detail and everything made sense, but these solutions did not work for me, because my primary key has no problem, and the transaction is managed by Spring, and it is OK in other save operations, but there is no problem.
Since there is no problem objectively, I focused on the specific objects to save or update, and carefully analyzed the POJO. First, let’s take a look at the tables in the corresponding database of the object to save:
/*==========================================================================================================================================/ create table product ( /* Product number, automatic growth */ id int primary key not null auto_increment, /* Product name*/ name varchar(50), /* Product price*/ price decimal(8,2), /* Product picture*/ pic varchar(300), /* Product brief introduction*/ remark longtext, /* Product details*/ xremark longtext, /* Product production date*/ date timestamp default CURRENT_TIMESTAMP, /* Whether it is a recommended product, it may be displayed on the home page of the mall */ comment bool, /* Whether it is a valid product, it may be displayed on the home page of the mall */ open bool, /* The category number of the product is located */ cid int, constraint cid_FK foreign key(cid) references category(id) );
Then the specific POJO will not be posted, but the field properties and set and get methods generated based on this table. I think the field that is most likely to have problems should be this time date, so I looked at the code about date in POJO:
@Entity public class Product implements java.io.Serializable { // Fields private Timestamp date; //Omit other irrelevant code... @Column(name = "date", nullable = false, length = 19) public Timestamp getDate() { return this.date; } public void setDate(Timestamp date) { this.date = date; } } So I searched for this Timestamp online again and found that the problem lies here, just change Timestamp to java.util.Date. Then a Date object is passed in, and Hibernate will automatically convert to Timestamp type.
This problem also gives me a revelation : the inability to perform database operations may also be a problem with the object itself, and we need to troubleshoot it between the table's fields and POJO attributes.
Original link: http://blog.csdn.net/eson_15/article/details/51383298
The above is all about this article, I hope it will be helpful to everyone's learning.