栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

您可以实现可配置接口,并根据需要覆盖配置。这样,您只能将静态值作为参数传递给

CourierTransImpl

如果要传递一些动态值,则可以

@Transient
在实体中定义一个属性,然后在
CourierTransImpl
类中访问该属性。

详细说明:

例如,让我们说有一个名为的实体

Employee
,它有一个名为的瞬时属性,
empType
然后您可以像这样定义该实体。

@Entitypublic 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”。

现在

CourierTransImpl
,实现Configurable接口的类:

public class CourierTransImpl implements IdentifierGenerator, Configurable {private String appendString;@Overridepublic 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 pre you can set the required value dynamically.        return appendString + emp.getEmpType()+id;    } catch (Exception e) {        e.printStackTrace();    }    return appendString + id;}@Overridepublic 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将生成和ID,例如“
Emp1Manager”。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417620.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号