gpt4 book ai didi

java - Hibernate Embedded 类作为 NaturalId

转载 作者:搜寻专家 更新时间:2023-10-30 23:41:36 26 4
gpt4 key购买 nike

我使用 Hibernate 4.1。
我在 Sanad 中有一个实体 Sanad 和一个嵌入式类 Gas

@Entity
public class Sanad{
@Id
private int id;

@NaturalId
@Embedded
Gas gas;
}


@Embeddable
@AttributeOverrides({
@AttributeOverride(name = "gate", column = @Column(name = "gate")),
@AttributeOverride(name = "serial", column = @Column(name = "serial"))
})
public class Gas{
int gate;
String serial;
}

我希望 Sanad 中的 gas 字段为 NaturalId。我该怎么做?
当我在 Sanad 的气田上方使用 @NaturalId 时,我在部署时遇到了这个问题:

Caused by: org.hibernate.MappingException: Unable to find logical column name from physical name gas in table Sanad
at org.hibernate.cfg.Configuration$MappingsImpl.getLogicalColumnName(Configuration.java:3258)
at org.hibernate.cfg.IndexOrUniqueKeySecondPass.doSecondPass(IndexOrUniqueKeySecondPass.java:83)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)

最佳答案

您可以将@NaturalId 注释添加到嵌入对象,如下所示:

@Entity
public class Sanad{
@Id
private int id;

@NaturalId
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "gate", column = @Column(name = "gate")),
@AttributeOverride(name = "serial", column = @Column(name = "serial"))
})
Gas gas;
}

@Embeddable
public class Gas{
@Column(name = "gate")
int gate;
@Column(name = "serial")
String serial;
}

看这里的例子:

https://docs.jboss.org/hibernate/orm/5.0/mappingGuide/en-US/html/ch07.html

关于java - Hibernate Embedded 类作为 NaturalId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34455736/

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