Hi.
I am trying to implement simple relationships between two classes User and Role. But I've got an error message when try
to save a user entity into a database. Could you please tell me what's is going on and why I get an error message?
Here is my code.
Table roles:
Code:
CREATE TABLE users.roles
(
id bigserial NOT NULL,
"name" character varying(255),
user_id bigint,
CONSTRAINT roles_pkey PRIMARY KEY (id)
)
Table users:
Code:
CREATE TABLE users.users
(
id bigserial NOT NULL,
create_date timestamp without time zone,
deleted character(1),
email character varying(255),
"name" character varying(255),
pass character varying(255),
state character varying(255),
CONSTRAINT users_pkey PRIMARY KEY (id)
)
Role.class:
Code:
@Entity
@Table(name = "users.roles")
public class Role implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id")
private Long userId;
@Column(name = "name")
private String name;
getters and setters here...
User.class:
Code:
@Entity
@Table(name = "users.users")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long id;
@Basic
@Column(name = "pass")
private String pass;
@Basic
@Column(name = "name")
private String name;
@Basic
@Column(name = "email")
private String email;
@Column(name = "deleted")
@Type(type = "true_false")
private boolean deleted;
@Column(name = "state")
private String state;
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "create_date")
private Date createDate;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Role> roles;
And here is my error:
Code:
922 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
INFO : [HibernateTransactionManager.afterPropertiesSet] Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@c1186f] of Hibernate SessionFactory for HibernateTransactionManager
Hibernate: insert into users.users (create_date, deleted, email, name, pass, state) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into users.roles (name, user_id) values (?, ?)
[b]Hibernate: insert into users.users_users.roles (users.users_user_id, roles_id) values (?, ?)[/b]
1563 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 0A000
1563 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Batch entry 0 insert into users.users_users.roles (users.users_user_id, roles_id) values ('35', '28') was aborted. Call getNextException to see the cause.
1578 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 0A000
1578 [main] ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: cross-database references are not implemented: "users.users_users.roles"
.ะพ.....: 13
1578 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
....
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into users.users_users.roles (users.users_user_id, roles_id) values ('35', '28') was aborted. Call getNextException to see the cause.
...
INFO : [XmlBeanDefinitionReader.loadBeanDefinitions] Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
INFO : [SQLErrorCodesFactory.<init>] SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
org.springframework.jdbc.UncategorizedSQLException: Hibernate flushing: Could not execute JDBC batch update; uncategorized SQLException for SQL [insert into users.users_users.roles (users.users_user_id, roles_id) values (?, ?)]; SQL state [0A000]; error code [0]; Batch entry 0 insert into users.users_users.roles (users.users_user_id, roles_id) values ('35', '28') was aborted. Call getNextException to see the cause.; nested exception is java.sql.BatchUpdateException: Batch entry 0 insert into users.users_users.roles (users.users_user_id, roles_id) values ('35', '28') was aborted. Call getNextException to see the cause.
....
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into users.users_users.roles (users.users_user_id, roles_id) values ('35', '28') was aborted. Call getNextException to see the cause.
Please tell me, why the hibernate try to insert data into
users.users_users.roles? As you can see I use a unidirectional association and they don't mean that I should have a share table to store user_id and role_id.
FYI: the property hibernate.hbm2ddl.auto is disabled(or use default value, it's not in the config)
Thank you.