我用Hibernate4.1。
我在Sanad中有一个实体Gas和一个嵌入类Sanad

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

我希望gas域内的Sanad被归化。我该怎么做?
当我在@NaturalId中使用Sanad以上气田时,我在部署时将面临以下情况:
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)

最佳答案

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

@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

10-06 12:50