Dúvida Iniciante - Relacionamento @OneToOne [RESOLVIDO]

4 respostas
Joao_Alonso

Estou iniciando meus estudos sobre o JPA quando fui iniciar os teste com Relacionamento já parei no primeiro exemplo dado rs.

Criei duas classes.

@Entity
@Table(name="riskbreaker")
public class Riskbreaker {

	@Id
	@GeneratedValue
	private Long id;
	private String nome;
@Entity
@Table(name="cadastro_jpa")
public class Cadastro {

	@Id
	@GeneratedValue
	private Long id;
	@Column(nullable=false)
	private String nome;
	private String email;
	@Transient
	private Long idade;
	@Temporal(TemporalType.DATE)
	@Column(nullable=true)
	private Calendar dataNascimento;
	@OneToOne
	private Riskbreaker riskbreaker;

e aqui está o persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="allstars" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<class>pojo.joey.Riskbreaker</class>
<properties>
<property name="hibernate.dialect"   value="org.hibernate.dialect.MySQLInnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />

<property name="javax.persistence.jdbc.driver"  value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"  value="jdbc:mysql://localhost/allstars" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="mkialonso123" />
</properties>
</persistence-unit>
</persistence>

e o erro gerado:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: allstars] Unable to configure EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:371) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) at teste.Teste.main(Teste.java:12) Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on pojo.joey.Cadastro.riskbreaker references an unknown entity: pojo.joey.Riskbreaker at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:103) at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:541) at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:523) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:380) at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1206) at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1449) at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1077) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:275) at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:359) ... 4 more

Se eu utilizo apenas uma classe como a anotação @Entity ele cria a tabela no bd naturalmente.
Os jars que estou utilizando são
hibernate3.Final
hibernate-jpa-2.0-api-1.0.1.Final
e todos arquivos da pasta required dos jars fornecidos no site do hibernate.

Alguém tem algum dica?

agradeço desde já.

4 Respostas

Hebert_Coelho

Você adicionou todas as classes no persistence? Parece que não.

Joao_Alonso

Adicione porém apresentou o mesmo erro:

<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="allstars" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>pojo.joey.Cadastro</class> <class>pojo.joey.Riskbreaker</class> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> ... </properties> </persistence-unit> </persistence>

Fiz um teste, eu tirei o objeto de Relacionamento mas mantive as duas classe com as anotaçõe Entitys. Aparentemente ele só enxerga a Classe Cadastro. Só criou essa tabela no BD pelo menos.

1 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.5.0-Final 25 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.0-Final 34 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found 40 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist 50 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling 231 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final 244 [main] INFO org.hibernate.ejb.Version - Hibernate EntityManager 3.5.0-Final 792 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: pojo.joey.Cadastro 912 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity pojo.joey.Cadastro on table cadastro_jpa 1010 [main] INFO org.hibernate.cfg.AnnotationConfiguration - Hibernate Validator not found: ignoring 1094 [main] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled. 1203 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!) 1203 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20 1203 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: true 1244 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/allstars 1244 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=root, password=****, autocommit=true, release_mode=auto} 1777 [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: MySQL, version: 5.1.63-0ubuntu0.11.10.1 1777 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.19 ( Revision: [email removido]-20111003110438-qfydx066wsbydkbw ) 1835 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLInnoDBDialect 1859 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory 1866 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) 1867 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled 1867 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled 1868 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15 1868 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled 1871 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled 1872 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled 1872 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto 1874 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2 1875 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1 1875 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled 1876 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled 1876 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled 1877 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 1882 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory 1883 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {} 1884 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: enabled 1884 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled 1885 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled 1885 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory 1886 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled 1887 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled 1895 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout 1906 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled 1906 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled 1907 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo 1907 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled 1907 [main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled 1996 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory 2267 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured 2277 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - Running hbm2ddl schema update 2277 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - fetching database metadata 2278 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - updating schema 2284 [main] INFO org.hibernate.tool.hbm2ddl.DatabaseMetadata - table not found: cadastro_jpa 2287 [main] INFO org.hibernate.tool.hbm2ddl.DatabaseMetadata - table not found: cadastro_jpa 2447 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - schema update complete 2710 [main] INFO org.hibernate.impl.SessionFactoryImpl - closing 2710 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:mysql://localhost/allstars

Hebert_Coelho

Quais são os imports do @Entity que você está utilizando? O mesmo para os dois?

Joao_Alonso

cara valeu mesmo, falta de atenção na hora de usar o altocomplete do Eclipse, rs. era isso mesmo.
valeu pela ajuda.

Criado 26 de junho de 2012
Ultima resposta 26 de jun. de 2012
Respostas 4
Participantes 2