Hello All,
I've been trying to map a Map. My own code did not work, so I've decided to use and example from the reference docs (
http://www.hibernate.org/hib_docs/annot ... ollections)
However, I did not manage to get that that to work either (obviously, I had to fill in the classes, but I think I did not modify any of the persistence annotations). The 'Software' object is persisted, none of the 'Versions' are. I do not see any place in the table where the key of 'myVersion' (a String) would be saved.
Any ideas?
TIA,
Kofa
DB structure:
Code:
+----------------+
| Tables_in_test |
+----------------+
| software |
| tbl_version |
+----------------+
mysql> desc software;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | | PRI | NULL | auto_increment |
| productName | varchar(255) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
mysql> desc tbl_version;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | | PRI | NULL | auto_increment |
| codeName | varchar(255) | YES | | NULL | |
| software_id | bigint(20) | YES | MUL | NULL | |
+-------------+--------------+------+-----+---------+----------------+
Source:Code:
@Entity
public class Software
{
private Long myId;
@Id @GeneratedValue (strategy=GenerationType.AUTO)
public Long getId() {return myId;}
private void setId(Long id) {myId = id;}
Map<String, Version> myVersions;
private String myProductName;
@OneToMany(mappedBy="software")
@MapKey(name="codeName")
public Map<String, Version> getVersions() {return myVersions;}
private void setVersions(Map<String, Version> versions) {myVersions = versions;}
private Software() {super();}
public Software(String productName)
{
myProductName = productName;
myVersions = new HashMap<String, Version>();
}
public String getProductName() {return myProductName;}
private void setProductName(String productName) {myProductName = productName;}
public void addVersion(String code, Version v) {myVersions.put(code, v);}
}
@Entity
@Table(name="tbl_version")
public class Version
{
private Long myId;
@Id @GeneratedValue (strategy=GenerationType.AUTO)
public Long getId() {return myId;}
private void setId(Long id) {myId = id;}
private String myCodeName;
public String getCodeName() {return myCodeName;}
public void setCodeName(String codeName) {myCodeName = codeName;}
private Software mySoftware;
@ManyToOne
public Software getSoftware() {return mySoftware;}
public void setSoftware(Software software) {mySoftware = software;}
private Version() {super();}
public Version(Software software, String codeName)
{
setSoftware(software); setCodeName(codeName);
}
}
Hibernate version:Hibernate 3.1.2 with Annotations 3.1beta-8
Code between sessionFactory.openSession() and session.close():Code:
Transaction tx = session.beginTransaction();
session.persist(s);
tx.commit();
:-)
Name and version of the database you are using:MySQL 4.0.13
The generated SQL (show_sql=true):11:13:20,700 DEBUG SchemaExport:296 - drop table if exists Software
11:13:20,716 DEBUG SchemaExport:296 - drop table if exists tbl_version
11:13:20,716 DEBUG SchemaExport:296 - create table Software (id bigint not null auto_increment, productName varchar(255), primary key (id)) type=MyISAM
11:13:20,716 DEBUG SchemaExport:296 - create table tbl_version (id bigint not null auto_increment, codeName varchar(255), software_id bigint, primary key (id)) type=MyISAM
11:13:20,732 DEBUG SchemaExport:296 - alter table tbl_version add index FK2A21E55761CD8148 (software_id), add constraint FK2A21E55761CD8148 foreign key (software_id) references Software (id)
11:13:20,778 INFO SchemaExport:202 - schema export complete
Hibernate: /* insert hu.kofa.Software */ insert into Software (productName) values (?)
Code: