public class User2
{
int id;
List<String> nickName = new ArrayList<String>();
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public List<String> getNickName()
{
return nickName;
}
public void setNickName(List<String> nickName)
{
this.nickName = nickName;
}
public class Test
{
public static void main(String[] args)
{
Configuration conf = new Configuration().configure();
SessionFactory f = conf.buildSessionFactory();
Session s = f.openSession();
Transaction t = s.beginTransaction();
User2 u = new User2();
u.setId(9);
List<String> list = new ArrayList<String>();
list.add("Jet");
list.add("hehe");
u.setNickName(list);
s.save(u);
t.commit();
s.close();
f.close();
}
}
提示错误是
ERROR: HHH000388: Unsuccessful: create table nickName (id integer not null, nick-name varchar(255), list-index integer not null, primary key (id, list-index)) type=InnoDB
三月 26, 2014 8:41:58 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-name varchar(255), list-index integer not null, primary key (id, list-index)) t' at line 1
三月 26, 2014 8:41:58 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000388: Unsuccessful: alter table nickName add constraint FK_5w1rlaf9gxhfepkgccwij41mp foreign key (id) references User2 (id)
三月 26, 2014 8:41:58 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: Table 'test.nickname' doesn't exist
三月 26, 2014 8:41:58 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: insert into User2 values ( )
三月 26, 2014 8:41:58 下午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1364, SQLState: HY000
三月 26, 2014 8:41:58 下午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Field 'id' doesn't have a default value
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement