I use hibernate 5.2.4.
When I try to use @NaturalId with @Embedded, there is an exception
(org.hibernate.MappingException: Unable to find column with physical name ...).
If I use @Embedded without @NaturalId all work fine
My code:
Code:
package com.slavick.hibernate.example;
import org.hibernate.annotations.NaturalId;
import javax.persistence.*;
@Entity
@Access(AccessType.FIELD)
public class Bar {
@Id
@GeneratedValue
long id;
@NaturalId
@Embedded
Foo foo;
public Bar(){};
}
package com.slavick.hibernate.example;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
@Access(AccessType.FIELD)
public class Foo {
@Column(name = "str")
String string;
public Foo(){}
}
package com.slavick.hibernate.example;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import java.util.Properties;
public class Program {
public static void main(String[] args) {
Properties prop = new Properties();
prop.setProperty("hibernate.connection.url", "jdbc:mysql://192.168.72.130:3306/test2");
prop.setProperty("hibernate.connection.username", "user");
prop.setProperty("hibernate.connection.password", "user");
prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");
prop.setProperty("hibernate.hbm2ddl.auto", "create-drop");
prop.setProperty("hibernate.show_sql", "true");
prop.setProperty("jdbc.driverClassName", "com.mysql.jdbc.Driver");
Configuration configuration = new Configuration().addProperties(prop).
addAnnotatedClass(Foo.class).addAnnotatedClass(Bar.class);
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
Foo foo = new Foo();
foo.string="string";
Bar bar = new Bar();
bar.foo=foo;
session.persist(bar);
session.close();
sessionFactory.close();
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}
STDOUT:
Quote:
"C:\Program Files (x86)\Java\jdk1.8.0_91\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:9989,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_91\jre\lib\rt.jar;C:\Users\apanasyonok\Downloads\HibernateExample\target\classes;C:\Users\apanasyonok\.m2\repository\mysql\mysql-connector-java\5.1.31\mysql-connector-java-5.1.31.jar;C:\Users\apanasyonok\.m2\repository\org\hibernate\hibernate-core\5.2.4.Final\hibernate-core-5.2.4.Final.jar;C:\Users\apanasyonok\.m2\repository\org\jboss\logging\jboss-logging\3.3.0.Final\jboss-logging-3.3.0.Final.jar;C:\Users\apanasyonok\.m2\repository\org\hibernate\javax\persistence\hibernate-jpa-2.1-api\1.0.0.Final\hibernate-jpa-2.1-api-1.0.0.Final.jar;C:\Users\apanasyonok\.m2\repository\org\javassist\javassist\3.20.0-GA\javassist-3.20.0-GA.jar;C:\Users\apanasyonok\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\apanasyonok\.m2\repository\org\apache\geronimo\specs\geronimo-jta_1.1_spec\1.1.1\geronimo-jta_1.1_spec-1.1.1.jar;C:\Users\apanasyonok\.m2\repository\org\jboss\jandex\2.0.0.Final\jandex-2.0.0.Final.jar;C:\Users\apanasyonok\.m2\repository\com\fasterxml\classmate\1.3.0\classmate-1.3.0.jar;C:\Users\apanasyonok\.m2\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;C:\Users\apanasyonok\.m2\repository\org\hibernate\common\hibernate-commons-annotations\5.0.1.Final\hibernate-commons-annotations-5.0.1.Final.jar;C:\Users\apanasyonok\.m2\repository\javax\enterprise\cdi-api\1.1\cdi-api-1.1.jar;C:\Users\apanasyonok\.m2\repository\javax\el\el-api\2.2\el-api-2.2.jar;C:\Users\apanasyonok\.m2\repository\org\jboss\spec\javax\interceptor\jboss-interceptors-api_1.1_spec\1.0.0.Beta1\jboss-interceptors-api_1.1_spec-1.0.0.Beta1.jar;C:\Users\apanasyonok\.m2\repository\javax\annotation\jsr250-api\1.0\jsr250-api-1.0.jar;C:\Users\apanasyonok\.m2\repository\javax\inject\javax.inject\1\javax.inject-1.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.5\lib\idea_rt.jar" com.slavick.hibernate.example.Program
Connected to the target VM, address: '127.0.0.1:9989', transport: 'socket'
но. 16, 2016 4:07:31 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.4.Final}
но. 16, 2016 4:07:31 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
но. 16, 2016 4:07:31 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
но. 16, 2016 4:07:31 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
но. 16, 2016 4:07:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
но. 16, 2016 4:07:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [null] at URL [jdbc:mysql://192.168.72.130:3306/test2]
но. 16, 2016 4:07:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=user, password=****}
но. 16, 2016 4:07:31 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
но. 16, 2016 4:07:31 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
но. 16, 2016 4:07:31 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Exception in thread "main" org.hibernate.MappingException: Unable to find column with physical name foo in table Bar
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getLogicalColumnName(InFlightMetadataCollectorImpl.java:1001)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getLogicalColumnName(InFlightMetadataCollectorImpl.java:971)
at org.hibernate.cfg.IndexOrUniqueKeySecondPass.doSecondPass(IndexOrUniqueKeySecondPass.java:69)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at com.slavick.hibernate.example.Program.main(Program.java:32)