Hi everybody !
I know this post is old but I have the same question as
rbecke !
I need to add automatically an
@GeneratedValue to all the primaty keys of all my tables. Here is the reason why :
- I managed to generate automatically my own OID using my specific class
OIDgenerator.javaCode:
public class OIDgenerator implements IdentifierGenerator {
public Serializable generate(SessionImplementor arg0, Object obj) throws HibernateException {
(...)
return OID;
}
}
- this class can be used for all the primary keys of the tables/classes of my package, thanks to the
package-info.java class :
Code:
@org.hibernate.annotations.GenericGenerator(name="OID", strategy="com.mySociety.myProject.hibernate.OIDgenerator")
package com.mySociety.myProject.bo;
- so now, if I manually add
@GeneratedValue before a given primary key, then my
OIDgenerator.java class is correctly used to generate the table primary key (when I add new records in the table) :
Code:
package com.mySociety.myProject.bo;
import (...);
@Entity
@Table(name = "T_POR", schema = "SCHEMA")
public class TPor implements java.io.Serializable {
private String porOid;
private String porAttr;
(...)
public TPor() {
}
public TPor(String porOid, String porAttr, (...) ) {
this.porOid = porOid;
this.porAttr = porAttr;
(...)
}
public TPor(String porOid, String porAttr, (...) ) {
this.porOid = porOid;
this.porAttr = porAttr;
(...)
}
@Id
@GeneratedValue <<--------------------------------------------------------------HERE---------------
@Column(name = "POR_OID", unique = true, nullable = false, length = 40)
public String getPorOid() {
return this.porOid;
}
public void setPorOid(String porOid) {
this.porOid = porOid;
}
@Column(name = "POR_ATTR", nullable = false, length = 3)
public String getPorAttr() {
return this.porAttr;
}
public void setPorAttr(String porAttr) {
this.porAttr = porAttr;
}
(...)
}
- I found a way to
roughly automatically generate something that is close to what I want to do : in the
hibernate.reveng.xml file, I added a primary key generator tag :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
<hibernate-reverse-engineering>
<schema-selection match-schema="SCHEMA" />
(...)
<table-filter match-schema="SCHEMA" match-name=".*"></table-filter>
(...)
<table schema="SCHEMA" name="T_POR">
<primary-key>
<generator class="com.mySociety.myProject.hibernate.OIDgenerator"/>
</primary-key>
</table>
</hibernate-reverse-engineering>
When I generate my classes with hibernate, I have this result for the table of the example above :
Code:
(...)
@GenericGenerator(name = "generator", strategy = "com.mySociety.myProject.hibernate.OIDgenerator")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "POR_OID", unique = true, nullable = false, length = 40)
public String getPorOid() {
return this.porOid;
}
(...)
I said this is
roughly automatic because I'd need to add the primary key generator tag in the
hibernate.reveng.xml file for
all the tables of my DB !
And I said it is close to what I want because I don't need to specify again the generator strategy, because it is set by default in all the package (see above).
I tried to use a joker character ( '*', '.*', ...) in the primary key generator tags of the
hibernate.reveng.xml file but it failed.
I red about the
ReverseEngineeringStrategy classes but I didn't find how to addapt them to my problem ...
Code:
public class ReverseStrategy extends DelegatingReverseEngineeringStrategy {
public ReverseStrategy(ReverseEngineeringStrategy delegate) {
super(delegate);
}
}
So if someone can help, I'd be grateful :)
Thanks in advance.
[EDIT]I work with Eclipse and hiberate 3.5.6
[/EDIT]