0
votes

hello i am trying to do one to one relationship in hibernate 4.0 with annotation and i having this error.

 Mar 15, 2014 11:45:09 PM
org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Mar 15, 2014 11:45:09 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.0.Final}
Mar 15, 2014 11:45:09 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 15, 2014 11:45:09 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 15, 2014 11:45:09 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
Mar 15, 2014 11:45:09 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Mar 15, 2014 11:45:09 PM org.hibernate.internal.util.xml.DTDEntityResolveresolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Mar 15, 2014 11:45:09 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Mar 15, 2014 11:45:09 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Mar 15, 2014 11:45:10 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export

alter table Student_Details.Employee 
    drop 
    foreign key FK_fv5220moxm8v5ehji5yi2y9w3

drop table if exists Student_Details.Employee

drop table if exists Student_Details.EmployeeDetail

drop table if exists Student_Details.Student

create table Student_Details.Employee (
    employeeid integer not null auto_increment,
    employeename varchar(255),
    empdetailid_fk integer,
    primary key (employeeid)
)

create table Student_Details.EmployeeDetail (
    employeedetailid integer not null auto_increment,
    city varchar(255),
    employeeDesgnation varchar(255),
    salary integer not null,
    primary key (employeedetailid)
)

create table Student_Details.Student (
    studentId integer not null,
    phoneNumber integer not null,
    studentName varchar(255),
    primary key (studentId)
)

alter table Student_Details.Employee 
    add constraint FK_fv5220moxm8v5ehji5yi2y9w3 
    foreign key (empdetailid_fk) 
    references Student_Details.EmployeeDetail (employeedetailid)
Mar 15, 2014 11:45:10 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Mar 15, 2014 11:45:10 PM         org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Mar 15, 2014 11:45:10 PM         org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplbuildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL[jdbc:mysql://localhost:3307]
Mar 15, 2014 11:45:10 PM     org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplbuildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
Mar 15, 2014 11:45:10 PM     org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplbuildCreator
INFO: HHH000006: Autocommit mode: false
Mar 15, 2014 11:45:10 PM     org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000115: Hibernate connection pool size: 2 (min=1)
Mar 15, 2014 11:45:11 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Mar 15, 2014 11:45:11 PM     org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Mar 15, 2014 11:45:11 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory<init>
INFO: HHH000397: Using ASTQueryTranslatorFactory

session opened Mar 15, 2014 11:45:12 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 1146, SQLState: 42S02 Mar 15, 2014 11:45:12 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: Table 'student_details.employeedetail' doesn't exist Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute statement at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190) at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3556) at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:97) at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:480) at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:191) at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:175) at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:210) at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288) at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:680) at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:672) at org.hibernate.engine.spi.CascadingActions$5.cascade(CascadingActions.java:235) at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:352) at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:295) at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118) at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:432) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265) at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209) at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194) at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:711) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:703) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:698) at com.exp.oneToOne.TestEmployee.main(TestEmployee.java:14) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'student_details.employeedetail' doesn't exist at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2459) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2376) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2360) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)

... 37 more

my code is this ::-

Employee.java

package com.exp.oneToOne;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Employee {

private int employeeid;
private String employeename;
private EmployeeDetail emp_details;

public Employee() {

}

public Employee(String employeename, EmployeeDetail emp_details) {

    this.employeename = employeename;
    this.emp_details = emp_details;
}



@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "empdetailid_fk")
public EmployeeDetail getEmp_details() {
    return emp_details;
}

public void setEmp_details(EmployeeDetail emp_details) {
    this.emp_details = emp_details;
}

@Id
@GeneratedValue
public int getEmployeeid() {
    return employeeid;
}

public void setEmployeeid(int employeeid) {
    this.employeeid = employeeid;
}

public String getEmployeename() {
    return employeename;
}

public void setEmployeename(String employeename) {
    this.employeename = employeename;
}

}

EmpolyeeDetails.java

package com.exp.oneToOne;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class EmployeeDetail {

private int employeedetailid;
private String employeeDesgnation;
private int salary;
private String city;

public EmployeeDetail() {       

}
public EmployeeDetail(String employeeDesgnation,
        int salary, String city) {


    this.employeeDesgnation = employeeDesgnation;
    this.salary = salary;
    this.city = city;
}

@Id
@GeneratedValue
public int getEmployeedetailid() {
    return employeedetailid;
}

public void setEmployeedetailid(int employeedetailid) {
    this.employeedetailid = employeedetailid;
}

public String getEmployeeDesgnation() {
    return employeeDesgnation;
}

public void setEmployeeDesgnation(String employeeDesgnation) {
    this.employeeDesgnation = employeeDesgnation;
}

public int getSalary() {
    return salary;
}

public void setSalary(int salary) {
    this.salary = salary;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}
}

TestEmpolyee.java

package com.exp.oneToOne;
import org.hibernate.Session;
import com.exp.first.MainConfig;
public class TestEmployee {
public static void main(String args[]) {
    Session session = MainConfig.mainsessionFactory().openSession();
    session.beginTransaction();
    EmployeeDetail empdetail = new EmployeeDetail("CEO", 250000,
            "Ahmedabad");
    Employee emp = new Employee("Chirag", empdetail);
    session.save(emp);
    session.getTransaction().commit();
    session.close();
}
}

hibernateconfigration file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3307</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hibernate.default_schema">Student_Details</property>
    <property name="current_session_context_class">thread</property>
    <property name="connection.pool_size">2</property>

</session-factory>

1

1 Answers

0
votes

You should use @Table(name="table-name") annotation with each of your @Entity class like

@Entity
@Table(name="employeedetail")
public class EmployeeDetail {

//Code inside your Entity class

}