Home > database >  Hibernate mysql one-to-many thrown exception
Hibernate mysql one-to-many thrown exception

Time:10-30

Import the Java. Util. HashSet;
Import the Java. Util. Set;

/* *
* @ author BieHongLi
*
*/
Public class Dept
{

Private int deptId;//department number
Private String the deptName.//department name

Private Set Emps.//departments corresponding multiple employees, that is, a one-to-many relationship between

//private Set Emps=new HashSet<> (a);//convenient assignment, here can be instantiated directly create

Public int getDeptId ()
{
Return deptId;
}

Public void setDeptId (int deptId)
{
Enclosing deptId=deptId;
}

Public String getDeptName ()
{
Return the deptName.
}

Public void setDeptName (String the deptName)
{
Enclosing the deptName=the deptName;
}

Public Set GetEmps ()
{
Return emps.
}

Public void setEmps (Set Emps)
{
Enclosing emps=emps;
}

}





Hibernate. CFG. XML

<? The XML version="1.0" encoding="utf-8"?>
"-//Hibernate/Hibernate Configuration DTD/3.0/EN"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" & gt;












A department may have many employees, an employee can only be in one department, departments and employees is a one-to-many relationship,



The test

The import org. Hibernate. Session;
Import org, hibernate SessionFactory;
The import org. Hibernate. Transaction;
The import org. Hibernate. CFG. Configuration;


Public class Test
{
Public static void main (String [] args)
{
The Configuration CFG=new Configuration (). The configure ();
SessionFactory sf=CFG. BuildSessionFactory ();
The Session Session.=sf openSession ();
The Transaction ts=session. BeginTransaction ();

Dept Dept=new Dept ();
Development dept. SetDeptName (" ");

The Employee emp1=new Employee ();
Emp1. SetEmpName (" aa ");

The session. The save (emp1);
The session. The save (dept);
Ts.com MIT ();
The session. The close ();
sf.close();
}
}


Abnormal

The Exception in the thread "main" javax.mail. Persistence. PersistenceException: org. Hibernate. Exception. SQLGrammarException: could not execute the statement
The at org. Hibernate. Internal. ExceptionConverterImpl. Convert (ExceptionConverterImpl. Java: 149)
The at org. Hibernate. Internal. ExceptionConverterImpl. Convert (ExceptionConverterImpl. Java: 157)
The at org. Hibernate. Internal. ExceptionConverterImpl. Convert (ExceptionConverterImpl. Java: 164)
The at org. Hibernate. Internal. SessionImpl. DoFlush (SessionImpl. Java: 1443)
The at org. Hibernate. Internal. SessionImpl. ManagedFlush (SessionImpl. Java: 493)
The at org. Hibernate. Internal. SessionImpl. FlushBeforeTransactionCompletion (SessionImpl. Java: 3207)
The at org. Hibernate. Internal. SessionImpl. BeforeTransactionCompletion (SessionImpl. Java: 2413)
The at org. Hibernate. Engine. JDBC. Internal. JdbcCoordinatorImpl. BeforeTransactionCompletion (JdbcCoordinatorImpl. Java: 467)
The at org. Hibernate. Resource. Transaction. Backend. JDBC. Internal. JdbcResourceLocalTransactionCoordinatorImpl. BeforeCompletionCallback (JdbcResourceLocalTransactionCoordinatorImpl. Java: 156)
The at org. Hibernate. Resource. Transaction. Backend. JDBC. Internal. JdbcResourceLocalTransactionCoordinatorImpl. Access the $100 (JdbcResourceLocalTransactionCoordinatorImpl. Java: 38)
At MIT (231) JdbcResourceLocalTransactionCoordinatorImpl. Java: org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.com
At MIT (68) TransactionImpl. Java: org.hibernate.engine.transaction.internal.TransactionImpl.com
At the Test. The main (Test. Java: 24)
Under Caused by: org. Hibernate. Exception. SQLGrammarException: could not execute the statement
The at org. Hibernate. Exception. Internal. SQLExceptionTypeDelegate. Convert (SQLExceptionTypeDelegate. Java: 63)
The at org. Hibernate. Exception. Internal. StandardSQLExceptionConverter. Convert (StandardSQLExceptionConverter. Java: 42)
The at org. Hibernate. Engine. JDBC. Spi. SqlExceptionHelper. Convert (SqlExceptionHelper. Java: 111)
The at org. Hibernate. Engine. JDBC. Spi. SqlExceptionHelper. Convert (SqlExceptionHelper. Java: 97)
The at org. Hibernate. Engine. JDBC. Internal. ResultSetReturnImpl. ExecuteUpdate (ResultSetReturnImpl. Java: 178)
The at org. Hibernate. Engine. JDBC. Batch. Internal. NonBatchingBatch. AddToBatch (NonBatchingBatch. Java: 45)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related