亲爱的开发者。
我要问你一个我认为很难为我解决的问题,因为我不知道基本的为什么要回答,我的意思是什么?让我们来看看。JBoss 文档中的关联没有得到全面的答案:为什么我应该使用 JoinTable 而不是外键,我不完全理解映射是如何工作的,这是什么意思?我知道什么是关联是 ManyToMany 或 ManyToOne 等,它们的目的是什么,但是它们如何工作和相互协作不需要关于双向或单向或 joinTable 或关联的答案,我想拥有链接,我可以在其中找到有关我的两个问题的完整信息:
1)为什么我应该使用 JoinTable 而不是外键????
2)实体如何工作和相互协作(没有解释什么是manyToMany等关联和双向或单向关联)???
所以,我有一段代码因为误解而卡住了,我只是想插入数据我是 MYSQL 数据库:Name_INSTITUTION 和 TYPE_NAME(机构类型):*
我的实体类:
@Entity @Table(name="INSTITUTION") public class Institution implements Serializable{ private static final long serialVersionUID = -7636394097858726922L; private int Id; @Id @GeneratedValue(strategy=IDENTITY) @Column(name="ID") public int getId() { return Id; } public void setId(int id) { Id = id; } private int Version; @javax.persistence.Version @Column(name="VERSION") public int getVersion() { return Version; } public void setVersion(int version) { Version = version; } private String Name_Institution; @Column(name="NAME_INSTITUTION") public String getName_Institution() { return Name_Institution; } public void setName_Institution(String name_Institution) { Name_Institution = name_Institution; } private Type type_inInstitution; @ManyToOne @Cascade(org.hibernate.annotations.CascadeType.ALL) @JoinTable(name="TYPE_INSTITUTION", joinColumns=@JoinColumn(name="INSTITUTION_ID"), inverseJoinColumns=@JoinColumn(name="TYPE_ID")) public Type getType_inInstitution() { return type_inInstitution; } public void setType_inInstitution(Type type_inInstitution) { this.type_inInstitution = type_inInstitution; }
}
我的第二个实体类:
@Entity @Table(name="TYPE") public class Type implements Serializable { private static final long serialVersionUID = -4246217431412815552L; private String type; public Type(){} public Type(String type) { this.type = type; } private int Type_Id; @Id @GeneratedValue(strategy=IDENTITY) @Column(name="ID") public int getType_Id() { return Type_Id; } public void setType_Id(int type_Id) { Type_Id = type_Id; } private String Type_Name; @Column(name="TYPE_NAME") public String getType_Name() { return Type_Name; } public void setType_Name(String type_Name) { Type_Name = type_Name; } private int Version; @Version @Column(name="VERSION") public int getVersion() { return Version; } public void setVersion(int version) { Version = version; } private Set<Institution> set_Institution_inType = new HashSet<Institution>(); @OneToMany @JoinTable(name="TYPE_INSTITUTION", joinColumns=@JoinColumn(name="TYPE_ID"), inverseJoinColumns=@JoinColumn(name="INSTITUTION_ID")) public Set<Institution> getSet_Institution_inType() { return set_Institution_inType; } public void setSet_Institution_inType(Set<Institution> set_Institution_inType) { this.set_Institution_inType = set_Institution_inType; } public void addType(Institution institution) { institution.setType_inInstitution(this); getSet_Institution_inType().add(institution); }
}
DAO 类:
@Repository("daoInsertDataInterface") @Transactional public class InsertDataService implements DaoInsertDataInterface{ private org.apache.commons.logging.Log log= LogFactory.getLog(InsertDataService.class); private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } @Resource(name="sessionFactory") public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public Institution insertData(Institution institution) { sessionFactory.getCurrentSession().saveOrUpdate(institution); log.info(institution.getId()); return institution; }
}
我的元数据配置:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>${jdbc.driverClassName}</value></property> <property name="url"><value>${jdbc.connectionValues}</value></property> <property name="username"><value>${jdbc.userName}</value></property> <property name="password"><value>${jdbc.password}</value></property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref ="sessionFactory"/> </bean> <context:property-placeholder location="connection.properties"/> <tx:annotation-driven transaction-manager="transactionManager"/> <context:component-scan base-package="edu.demidov.dom, edu.demidov.dao" /> <context:annotation-config /> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref ="dataSource"/> <property name="packagesToScan" value="edu.demidov.dom"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.max_fetch_depth">3</prop> <prop key="hibernate.jdbc.fetch_size">50</prop> <prop key="hibernate.jdbc.batch_size">10</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
和错误:
信息:org.springframework.beans.factory.xml.XmlBeanDefinitionReader - 从类路径资源 [app-context.xml] 加载 XML bean 定义信息: org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named ' 找到并支持组件扫描 INFO 的注释:org.springframework.context.support.GenericXmlApplicationContext - 刷新 org.springframework.context.support.GenericXmlApplicationContext@c93274:启动日期 [Thu May 09 11:38:23 EDT 2013];上下文层次结构根信息:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer - 从类路径资源加载属性文件 [connection.properties] 信息:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.注入.注入'
工厂层次结构 Hibernate 的根:插入类型(TYPE_NAME,VERSION)值(?,?)警告:org.hibernate.util.JDBCExceptionReporter - SQL 错误:1048,SQLState:23000 错误:org.hibernate.util.JDBCExceptionReporter - 列' TYPE_NAME' 不能 为空线程“主”org.hibernate.exception.ConstraintViolationException 中的异常:无法插入:org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) 处的 [edu.demidov.dom.Type] org.hibernate .exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 在 org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2327) ) 在 org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) 在 org.hibernate.engine.ActionQueue.execute(ActionQueue. java:273) 在 org.hibernate.event.def。AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129) at org .hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) 在 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) 在 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener .java:117) 在 org.hibernate.impl 的 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)。org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677) 的 org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252) 的 org.hibernate 的 SessionImpl.fireSaveOrUpdate(SessionImpl.java:685)。 engine.Cascade.cascadeToOne(Cascade.java:392) at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335) at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204) at org.hibernate .engine.Cascade.cascade(Cascade.java:161) 在 org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:450) 在 org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:282 ) 在 org.hibernate.event.def 的 org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)。AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org .hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java :685) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:673) at edu.demidov.dao。InsertDataService.insertData(InsertDataService.java:32) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: 43) 在 java.lang.reflect.Method.invoke(Method.java:601) 在 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint (ReflectiveMethodInvocation.java:183) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 在 org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) 在 org.springframework .transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java: 172) 在 edu.demidov.dao.AppTEst.main(AppTEst.java:22) 的 $Proxy22.insertData(Unknown Source) 处的 org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at $Proxy22.insertData(Unknown Source) at edu.demidov.dao.AppTEst.main(AppTEst.java:22)java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at $Proxy22.insertData(Unknown Source) at edu.demidov.dao.AppTEst.main(AppTEst.java:22) 引起:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'TYPE_NAME'不能为空 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor .newInstance(Constructor.java:525) 在 com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 在 com.mysql.jdbc.Util.getInstance(Util.java:386) 在 com.mysql.jdbc。 SQLError.createSQLException(SQLError.java:1041) 在 com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120) 在 com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052) 在 com.mysql.jdbc .MysqlIO.sendCommand(MysqlIO.java:2503) 在 com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664) 在 com.mysql.jdbc.ConnectionImpl。execSQL(ConnectionImpl.java:2815) 在 com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) 在 com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458) 在 com.mysql.jdbc.PreparedStatement .executeUpdate(PreparedStatement.java:2375) 在 org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94) 在 org.hibernate 的 com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)。 id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) ... 44 更多 SSexecuteUpdate(PreparedStatement.java:2375) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359) at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94) at org.hibernate.id .insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) ... 44 更多 SSexecuteUpdate(PreparedStatement.java:2375) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359) at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94) at org.hibernate.id .insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) ... 44 更多 SS
感谢你们。