Hibernate Embedded类为NaturalId

时间:2015-12-24 16:50:48

标签: java database hibernate orm entity

我使用Hibernate 4.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。我怎么能这样做?
当我在@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)

2 个答案:

答案 0 :(得分:1)

您可以将@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

答案 1 :(得分:0)

我认为问题与该Hibernate错误中报告的问题相同

https://hibernate.atlassian.net/browse/HHH-4249