Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
Hibernate 3.1
Mapping documents:
Hi, I don't know if I get it right but...
I am dealing with a simple association between two entities:
Code:
|Server|1------>1..*|Authorities|
then my classes look like:
Code:
public class Server implements java.io.Serializable {
public Server(String name, String address) {
this.name = name;
this.address = address;
authorities = new HashSet<Authority>();
}
@Id
public String getName() {
return name;
}
@OneToMany( mappedBy = "server" )
public Set<Authority> getAuthorities() {
return authorities;
}
...
}
public class Authority implements java.io.Serializable {
@Id(generate = GeneratorType.IDENTITY)
@Column(name = "AUTHORITY_ID")
public int getAuthorityId() {
return authorityId;
}
@ManyToOne(
cascade = {CascadeType.ALL}
)
@JoinColumn(name="SERVER_NAME")
public Server getServer() {
return server;
}
...
}
What I have been trying to do is the following:
Code:
Server s1 = new Server(...);
Authority a1=new Authority();
a1.setServer(s1);
session.save(a1);
Server sFound = session.load(Server.class, s1.getName());
assertNotNull(sFound.getAuthorities());
when I store the Authority a1 the reference to the Server instance s1 is made in the table (through the FK).
What I was expecting is that when loading a Server instance that is referenced by some Authority record (such a1) the authorities Set was filled...
Instead this happens only if I do
Code:
Server s1 = new Server(...);
Authority a1 = new Authority();
a1.setServer(s1);
s1.getAuthorities().add(a1);
session.save(a1);
I have read in Hibernate in action that
"Hibernate doesn't manage persistent associations"
But I was hoping/expecting that this behavior wasn't applying when looking up instances...After all the left outer join is there just for this reason.
Name and version of the database you are using:HSQLDB 1.8.0
The generated SQL (show_sql=true):Code:
15:54:41,117 DEBUG SchemaExport:296 - create table AUTHORITIES (AUTHORITY_ID integer generated by default as identity (start with 1), SERVER_NAME varchar(255), DOMAIN_LABEL varchar(255), primary key (AUTHORITY_ID), unique (SERVER_NAME, DOMAIN_LABEL))
15:54:41,129 DEBUG SchemaExport:296 - create table DOMAINS (label varchar(255) not null, primary key (label))
15:54:41,131 DEBUG SchemaExport:296 - create table PROBE_DEFINITIONS (definitionId integer generated by default as identity (start with 1), frequency integer not null, attempts integer not null, timeout integer not null, QUERY_TYPE_CODE integer, AUTHORITY_ID integer, primary key (definitionId), unique (AUTHORITY_ID, QUERY_TYPE_CODE))
15:54:41,134 DEBUG SchemaExport:296 - create table QUERY_TYPES (code integer not null, type varchar(255), primary key (code))
15:54:41,135 DEBUG SchemaExport:296 - create table SERVERS (name varchar(255) not null, address varchar(255), primary key (name))
15:54:41,139 DEBUG SchemaExport:296 - alter table AUTHORITIES add constraint FKAB62A701681F199A foreign key (SERVER_NAME) references SERVERS
15:54:41,143 DEBUG SchemaExport:296 - alter table AUTHORITIES add constraint FKAB62A7013C90BCCD foreign key (DOMAIN_LABEL) references DOMAINS
15:54:41,146 DEBUG SchemaExport:296 - alter table PROBE_DEFINITIONS add constraint FKBDE05F315C3B908A foreign key (AUTHORITY_ID) references AUTHORITIES
15:54:41,153 DEBUG SchemaExport:296 - alter table PROBE_DEFINITIONS add constraint FKBDE05F31F30586ED foreign key (QUERY_TYPE_CODE) references QUERY_TYPES
15:54:41,157 INFO SchemaExport:202 - schema export complete
15:54:41,161 INFO Configuration:1022 - processing extends queue
15:54:41,162 INFO Configuration:1026 - processing collection mappings
15:54:41,164 INFO Configuration:1035 - processing association property references
15:54:41,164 INFO Configuration:1057 - processing foreign key constraints
15:54:41,166 INFO Configuration:1022 - processing extends queue
15:54:41,166 INFO Configuration:1026 - processing collection mappings
15:54:41,167 INFO Configuration:1035 - processing association property references
15:54:41,168 INFO Configuration:1057 - processing foreign key constraints
15:54:41,171 INFO SessionFactoryImpl:353 - Checking 0 named HQL queries
15:54:41,172 INFO SessionFactoryImpl:373 - Checking 0 named SQL queries
Hibernate: insert into DOMAINS (label) values (?)
Hibernate: insert into SERVERS (address, name) values (?, ?)
Hibernate: select server_.name, server_.address as address4_ from SERVERS server_ where server_.name=?
Hibernate: select domain_.label from DOMAINS domain_ where domain_.label=?
Hibernate: insert into AUTHORITIES (SERVER_NAME, DOMAIN_LABEL, AUTHORITY_ID) values (?, ?, null)
Hibernate: call identity()
Hibernate: insert into SERVERS (address, name) values (?, ?)
Hibernate: insert into DOMAINS (label) values (?)
Hibernate: insert into DOMAINS (label) values (?)
Hibernate: insert into AUTHORITIES (SERVER_NAME, DOMAIN_LABEL, AUTHORITY_ID) values (?, ?, null)
Hibernate: call identity()
Hibernate: insert into AUTHORITIES (SERVER_NAME, DOMAIN_LABEL, AUTHORITY_ID) values (?, ?, null)
Hibernate: call identity()
Hibernate: select this_.AUTHORITY_ID as AUTHORITY1_0_2_, this_.SERVER_NAME as SERVER2_0_2_, this_.DOMAIN_LABEL as DOMAIN3_0_2_, server2_.name as name4_0_, server2_.address as address4_0_, domain3_.label as label1_1_ from AUTHORITIES this_ left outer join SERVERS server2_ on this_.SERVER_NAME=server2_.name left outer join DOMAINS domain3_ on this_.DOMAIN_LABEL=domain3_.label where this_.SERVER_NAME=? and this_.DOMAIN_LABEL=?
Hibernate: select this_.AUTHORITY_ID as AUTHORITY1_0_2_, this_.SERVER_NAME as SERVER2_0_2_, this_.DOMAIN_LABEL as DOMAIN3_0_2_, server2_.name as name4_0_, server2_.address as address4_0_, domain3_.label as label1_1_ from AUTHORITIES this_ left outer join SERVERS server2_ on this_.SERVER_NAME=server2_.name left outer join DOMAINS domain3_ on this_.DOMAIN_LABEL=domain3_.label where this_.SERVER_NAME=? and this_.DOMAIN_LABEL=?
[/code]