gpt4 book ai didi

java - 发送字符串第二个参数 IdentifierGenerator - Hibernate

转载 作者:太空宇宙 更新时间:2023-11-04 14:25:12 24 4
gpt4 key购买 nike

我已经为我的应用程序实现了一个自定义生成器,并且我想将一个字符串作为第二个参数发送到 IdentifierGenerator 接口(interface),但我没有得到任何关于如何执行此操作的线索。不幸的是,因为下面的代码,它将 null2 设置为生成的 key 。请帮忙。

我想发送一个字符串,它是来自客户端的“日期”作为第二个参数。

谢谢。

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;
}

}

最佳答案

您可以实现Configurable接口(interface)并覆盖 configure为了您的要求。通过这样做,您只能将静态值作为参数传递给 CourierTransImpl

如果您想传递一些动态值,则可以在实体中定义 @Transient 属性,然后在 CourierTransImpl 类中访问该属性。

详细说明:

例如,假设有一个名为 Employee 的实体,它有一个名为 empType 的 transient 属性,那么您可以像这样定义该实体。

@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
}

在上面的代码中,您可以看到我们设置了参数appendString,这是一个静态值,我们在此处设置为“Emp”。

现在是实现 Configurable 接口(interface)的 CourierTransImpl 类:

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

}

在这个例子中,如果我创建一个Employee对象并将empType设置为某个值,比如“Manager”,那么hibernate会生成像“Emp1Manager”这样的id。

关于java - 发送字符串第二个参数 IdentifierGenerator - Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26755942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com