Send String second参数IdentifierGenerator - Hibernate [英] Send String second Argument IdentifierGenerator - Hibernate
问题描述
我已经为我的应用程序实现了一个自定义生成器,并且我想将一个字符串作为第二个参数发送给IdentifierGenerator接口,但我不知道如何执行此操作。不幸的是,因为下面的代码,它将null2设置为生成的密钥。请帮助。
我想发送一个字符串,它是客户端的日期作为第二个参数。
感谢。
public class CourierTransImpl implements IdentifierGenerator {
private String appendString;
@Override
public Serializable generate(SessionImplementor session,Object arg1)
throws HibernateException {
Connection connection = session.connection();
int id = 0;
try {
PreparedStatement ps = connection
.prepareStatement(SELECT MAX(TRANS_ID)as SecurePass.COURIER_TRANSACTIONS_SER_TABLE);
ResultSet rs = ps.executeQuery();
if(rs.next()){
id = rs.getInt(value);
id ++;
}
ps = connection
.prepareStatement(INSERT INTO SecurePass.COURIER_TRANSACTIONS_SER_TABLE VALUES(+ id +));
ps.execute();
} catch(SQLException e){
e.printStackTrace();
}
return appendString + id;
}
public String getAppendString(){
return appendString;
}
public void setAppendString(String appendString){
this.appendString = appendString;
}
}
您可以执行可配置接口并覆盖配置以满足您的需求。通过这样做,您只能将一个静态值作为参数传递给 CourierTransImpl
class
如果您想传递一些动态值,则可以在实体中定义一个 @Transient
属性,然后在 CourierTransImpl
类中访问该属性。
详细解释:
例如,让我们说有一个实体调用 Employee
,它有一个名为 empType
的临时属性,那么您可以像这样定义实体。 @Entity
public class Employee {
@Id
@GeneratedValue(generator =UniqueIdGenerator )
@GenericGenerator(name =UniqueIdGenerator,strategy =com.CourierTransImpl,
parameters = {@Parameter(name =appendString,value =Emp)})
私人字符串ID;
私人字符串名称;
@Transient
private String empType;
// Getters& Setters
}
在上面的代码中,您可以看到我们设置了参数 appendString
这是一个静态值,我们在这里设置为Emp。
现在
public class CourierTransImpl implements IdentifierGenerator,Configurable {
private String appendString;
$ b $ @Override
public Serializable generate(SessionImplementor session,Object object)
throws HibernateException {
Connection connection = session.connection();
int id = 0;
尝试{
Employee emp =(Employee)object;
id = ..; //你的逻辑从数据库中获取id
//现在你可以使用参数appendString,它被静态设置为Emp
//你也可以访问任何员工属性,所以在你的代码中你可以动态地设置所需的值。
return appendString + emp.getEmpType()+ id;
} catch(Exception e){
e.printStackTrace();
}
return appendString + id;
$ b @Override
public void configure(Type type,Properties params,Dialect d)
throws MappingException {
setAppendString(params.getProperty( appendString)); //这里我们正在设置参数。
}
// Setters& Getters
}
我创建了一个 Employee
的对象,并将 empType
设置为某个值,如Manager,然后hibernate生成id比如Emp1Manager。
I have implemented a custom generator, for my application and I want to send a string as the second argument to the IdentifierGenerator interface but I am not getting any clue how to do this. unfortunately because fo the below code, it is setting null2 as the key generated. please help.
I want to send a String which is the "date" from the client as the second argument.
Thanks.
public class CourierTransImpl implements IdentifierGenerator{
private String appendString;
@Override
public Serializable generate(SessionImplementor session, Object arg1)
throws HibernateException {
Connection connection = session.connection();
int id=0;
try {
PreparedStatement ps = connection
.prepareStatement("SELECT MAX(TRANS_ID) as value from SecurePass.COURIER_TRANSACTIONS_SER_TABLE");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
id = rs.getInt("value");
id++;
}
ps = connection
.prepareStatement("INSERT INTO SecurePass.COURIER_TRANSACTIONS_SER_TABLE VALUES("+id+")");
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
return appendString+id;
}
public String getAppendString() {
return appendString;
}
public void setAppendString(String appendString) {
this.appendString = appendString;
}
}
You can implement the Configurable interface and override the configure for your requirement. By doing this you can only pass a static value as a parameter to CourierTransImpl
class
If you want to pass some dynamic values then you can have a @Transient
property defined in your entity and then access that property in your CourierTransImpl
class.
Detailed explanation:
For example, lets says there is an entity called Employee
and it has a transient property called empType
then you can define the entity like this.
@Entity
public class Employee {
@Id
@GeneratedValue(generator = "UniqueIdGenerator")
@GenericGenerator(name = "UniqueIdGenerator", strategy = "com.CourierTransImpl",
parameters = { @Parameter(name = "appendString", value = "Emp") })
private String id;
private String name;
@Transient
private String empType;
// Getters & Setters
}
In above code you can see that we set the parameter appendString
and this is a static value that we are setting here as "Emp".
Now the CourierTransImpl
class that implements Configurable interface:
public class CourierTransImpl implements IdentifierGenerator, Configurable {
private String appendString;
@Override
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
Connection connection = session.connection();
int id = 0;
try {
Employee emp = (Employee) object;
id = ..; // your logic to get the id from database
// Now you can use the parameter appendString which is static value set to "Emp"
// You can also access any of the employee properties here, so in your code you can set the required value dynamically.
return appendString + emp.getEmpType()+id;
} catch (Exception e) {
e.printStackTrace();
}
return appendString + id;
}
@Override
public void configure(Type type, Properties params, Dialect d)
throws MappingException {
setAppendString(params.getProperty("appendString")); // Here we are setting the parameters.
}
// Setters & Getters
}
In this example if I create an object of Employee
and set the empType
to some value say "Manager", then the hibernate generates and id like "Emp1Manager".
这篇关于Send String second参数IdentifierGenerator - Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!