Reputation: 298
I have following AbstractCompany super class entity:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class AbstractCompany {
@Id
@GenericGenerator(name = "rju_id", strategy = "com.ivc.talonsystem.util.keygens.RjuIdGenerator")
@GeneratedValue(generator = "rju_id")
@Column(name = "id")
private Integer id;
// other properties and getters-setters for them
}
and child class Rju:
@Entity
@Table(name="rju")
public class Rju extends AbstractCompany implements Serializable {
@NotEmpty
@Column(name = "fullname")
private String namerju;
@NotEmpty
@Column(name = "briefname")
private String briefname;
// other properties and getters-setters for them
}
and RjuIdGenerator class for generating id for Rju entity:
public class RjuIdGenerator implements IdentifierGenerator {
@Override
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
Connection connection = session.connection();
try {
Statement statement=connection.createStatement();
ResultSet rs=statement.executeQuery("select max(id) as Id from Rju");
if(rs.next())
{
int id=rs.getInt(1)+1;
Integer generatedId = new Integer(id);
return generatedId;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
this RjuIdGenerator class generates id for all super and child classes. How I can add another generator classes for specific child entities? Thanks in advance!!!
Upvotes: 1
Views: 555
Reputation: 298
Guys I've found a workaround for the question. All I did is check for instance of the object in the RjuIdGenerator's generate method then get back generated id for specific class using appropriate table querying:
public class RjuIdGenerator implements IdentifierGenerator {
@Override
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
Connection connection = session.connection();
try {
Statement statement=connection.createStatement();
if (object.getClass().getName()==Rju.class.getName()) {
ResultSet rs=statement.executeQuery("select max(id) as Id from Rju");
if(rs.next())
{
int id=rs.getInt(1)+1;
Integer generatedId = new Integer(id);
return generatedId;
}
} else {
ResultSet rs=statement.executeQuery("select max(id) as Id from AbstractCompany");
if(rs.next())
{
int id=rs.getInt(1)+100000;
Integer generatedId = new Integer(id);
return generatedId;
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
Upvotes: 1