Author:hiu
Recently changed from Ibatis to Hibernate, encountered a series of problems, and today, there is a problem, because the problem of self-added column caused by saving the Times wrong, now recorded, so that later forgotten when the view
This problem is actually caused by the annotated error of the self-increment column when using MyEclipse to generate the entity class for the table.
Cause: The entity class self-increment annotation generated using the MyEclipse hibernate Reverse engineening is set on the primary key keyjobno instead of the corresponding ID column
Workaround: set the self-increment annotation to the corresponding self-increment column ID
Database Employee table:
Here is the main list of some of the fields, the key is that the ID and Keyjobno,id set is the self-increment column, Keyjobno set as the primary key
Generating entity classes using MyEclipse's hibernate Reverse engineening
Generated entity class employee, only posted Keyjobno,id, others not posted
@Entity @table (name = "Employee", Catalog = "Union_ssh", uniqueconstraints = @UniqueConstraint (ColumnNames = "id")) public Class Employee implements Java.io.Serializable {//fieldsprivate String keyjobno;private Integer id; @Id @generatedvalue ( Strategy = IDENTITY) @Column (name = "Keyjobno", unique = true, Nullable = false, length =) public String getkeyjobno () {R Eturn This.keyjobno;} public void Setkeyjobno (String keyjobno) {this.keyjobno = Keyjobno;} @Column (name = "id", unique = true, Nullable = false) public Integer GetId () {return this.id;} public void SetId (Integer id) {this.id = id;}}
Service Layer
Public employee Save (employee employee) throws SQLException { employee.setkeyjobno ("K8888"); Employeedao.save (employee); return employee;}
DAO layer
@Overridepublic Serializable Save (T o) { return this.getcurrentsession (). Save (O);
SQL statement printed by the console:
2014-07-04 14:10:50 [org.hibernate.sql]-[debug] insert INTO Union_ssh.employee (Born_boon, Borndate, Company, CreateDate, dept, Getborn_date, getmarry_date, id, Identitycard, ifout, interest, jobno, lodging, Marry, Marry_b Oon, name, Nativename, operator, phone, politicsface, sex) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?, ?, ?, ?, ?, ?) Hibernate:insert to Union_ssh.employee (Born_boon, Borndate, Company, CreateDate, dept, Getborn_d Ate, getmarry_date, ID, Identitycard, ifout, interest, jobno, lodging, Marry, Marry_boon, name, Nativename, operator, Phon E, politicsface, Sex) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) 2014-07-04 14:10:50 [org.hibernate.engine.jdbc.spi.sqlexceptionhelper]-[debug] Field ' keyjobno ' doesn ' t have a default Value [N/a]<span style= "color: #ff0000;" ><strong>java.sql.sqlexception:field ' keyjobno ' doesn ' t have a default VALUE</STRONG></SPAn>
Notice the red part, the error said I keyjobno no default value, the strangest is here, I clearly in the service layer set up
Employee.setkeyjobno ("K8888");
Obviously already set the value for the keyjobno, actually also said I did not have the default value. Another look at the SQL statement, there is an ID, but no keyjobno, supposedly ID in the database table set the self-increment, here should not be inserted, but Keyjobno is already set the value, but not shown in the inserted SQL, This is because the annotations are set keyjobno in order to self-increment the columns.
Now take a look at the annotations in the entity class:
@Id @generatedvalue (strategy = IDENTITY) @Column (name = "Keyjobno", unique = true, Nullable = false, length =) public Stri Ng Getkeyjobno () {<span style= "white-space:pre" ></span>return this.keyjobno;}
remark Meaning:
@Id-the annotation declares the identity attribute of the entity bean (the primary key in the corresponding table).
@GeneratedValue-the annotation declares the generation strategy of the primary key. The annotation has the following properties
strategy Specifies the generated policy (JPA-defined), which is a generationtype. The default is GenerationType. auto
Generationtype.auto primary key controlled by program
Generationtype.identity primary key is automatically generated by the database (mainly auto-grow type)
See note Description, you should understand, I set in the database table is the ID self-increment column, but the generated entity class is keyjobno, this is the reason why the save failed, the entity class is changed to the following form, mainly the keyjobno of the self-increment annotations to the ID column
@Entity @table (name = "Employee", Catalog = "Union_ssh", uniqueconstraints = @UniqueConstraint (ColumnNames = "id")) public Class Employee implements Java.io.Serializable {private String keyjobno;private Integer ID; @Column (name = "Keyjobno", Uni Que = true, nullable = false, length = () public String getkeyjobno () {return this.keyjobno;} public void Setkeyjobno (String keyjobno) {this.keyjobno = Keyjobno;} <strong> @Id @generatedvalue (strategy = IDENTITY) </strong> @Column (name = "Id", unique = true, Nullable = False ) public Integer GetId () {return this.id;} public void SetId (Integer id) {this.id = id;}}
Redeploy, and then save, the console prints out the SQL statement:
2014-07-04 14:30:21 [Org.hibernate.sql]-[debug] insert INTO Union_ssh.employee (Born_boon, Borndate, Company, C Reatedate, Dept, Getborn_date, Getmarry_date, Identitycard, ifout, interest, jobno, keyjobno, lodging, Marry, Marry_boon, Name, Nativename, operator, phone, politicsface, sex) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?, ?, ?, ?, ?, ?) Hibernate:insert to Union_ssh.employee (Born_boon, Borndate, Company, CreateDate, dept, Getborn_d Ate, Getmarry_date, Identitycard, ifout, interest, jobno, keyjobno, lodging, Marry, Marry_boon, name, Nativename, operator , phone, politicsface, sex) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) 2014-07-04 14:30:21 [Org.hibernate.id.identifiergeneratorhelper]-[debug] natively generated identity:557
The last line natively generated identity:557 this sentence is what meaning, in fact, 557 is the ID of the inserted record, because I have 556 records in the database, which means that the save is successful, you can see, this SQL statement has no ID saved , that is, when the SQL statement is inserted without inserting the ID, it is automatically incremented in the database.
Summary: The error in the annotation place is because the annotations caused by using MyEclipse to generate the entity class are set local errors, so it is sometimes not too dependent on the automatic generation of entity classes. Personal guess because I set the ID to be self-increment, but the Keyjobno column is set as the primary key, that is to say, these two columns are unique, myeclipse in the generated entity class is possible can not distinguish, the self-added column is set in the column of the primary key, not carefully studied, we are interested, Can go under the study.