简体   繁体   中英

Hibernate Embedded class as NaturalId

I use Hibernate 4.1.
I have an entity Sanad and an embedded class Gas inside 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;
}

I want gas field inside Sanad to be NaturalId. How can I do that?
When I use @NaturalId above gas field in Sanad , I face this expcetion at deploy time:

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)

You can add the @NaturalId annotation to an embedded object as follows:

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

See the example here:

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

I think that the problem is the same that is reported in this Hibernate bug

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM