sorry about wot i did, my code is here:
first, I defined a table in MySQL:
Code:
CREATE TABLE IF NOT EXISTS users(
userID INT NOT NULL,
account VARCHAR(20) NOT NULL UNIQUE,
password VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
description TEXT NULL,
gender CHAR(1) NOT NULL, #m: male; f: female
dob DATE NULL,
CONSTRAINT user_pk PRIMARY KEY (userID),
INDEX (role),
CONSTRAINT user_fk FOREIGN KEY (role) REFERENCES roles(role)
) TYPE=InnoDB;
correspondent Bean is:
Code:
public class User
implements Serializable {
private Integer userID;
private String account;
private String password;
private String email;
private String description;
private char gender;
private String dob;
public User() {
}
public User(String account, String password, String email, char gender,
int role) {
this.account = account;
this.showname = account;
this.password = password;
this.email = email;
this.gender = gender;
this.role = role;
}
/**
* set user's ID
* @param userID String user's ID
*/
private void setUserID(Integer userID) {
this.userID = userID;
}
public Integer getUserID() {
return userID;
}
............Accessor Methods.....................
Mapping file for this Bean:
Code:
<class name="com.element.photohost.business.object.User" table="users">
<!--=====Identify definition===========-->
<id name="userID" column="userID">
<generator class="native"/>
</id>
<!--======property mapping=======-->
<property name="account" not-null="true" update="false">
<column name="account" length="20" sql-type="string" unique="true"/>
</property>
<property name="password" not-null="true">
<column name="password" sql-type="string" length="20"/>
</property>
................Normal property mapping statement............
the last part, I defined my hibernate.cfg.xml file like this:
Code:
<hibernate-configuration>
<session-factory name="java:/hibernate/HibernateFactory">
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/photobank</property>
<property name="connection.username">phAdmin</property>
<property name="connection.password">phpass</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.timeout">500</property>
<property name="c3p0.max_statements">100</property>
<property name="c3p0.idle_test_period">3000</property>
<property name="c3p0.acquire_increment">2</property>
<property name="proxool.pool_alias">pool1</property>
<property name="jdbc.batch_versioned_data">true</property>
<property name="jdbc.use_streams_for_binary">true</property>
<property name="max_fetch_depth">2</property>
<property name="cache.region_prefix">hibernate.test</property>
<property name="cache.use_query_cache">true</property>
<property name="cache.provider_class">net.sf.hibernate.cache.EhCacheProvider</property>
<mapping resource="com/element/photohost/business/object/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
in my program,
Code:
sessionFactory = configuration.configure().buildSessionFactory();
session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
User user = new User("account01", "password01", "email01", 'm', 1);
session.save(user);
session.flush();
tx.commit();
when i run this code, output error message is:
Quote:
Hibernate: insert into users (account, showname, password, defaultIcon, email, description, gender, dob, role) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
17:40:59,543 WARN JDBCExceptionReporter: SQL Error: 1216, SQLState: 23000
17:40:59,543 ERROR JDBCExceptionReporter: Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails"
17:40:59,553 WARN JDBCExceptionReporter: SQL Error: 1216, SQLState: 23000
17:40:59,553 ERROR JDBCExceptionReporter: Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails"
17:40:59,553 ERROR JDBCExceptionReporter: could not insert: [com.element.photohost.business.object.User]
java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails"
[/code]