gpt4 book ai didi

java - 列不能为空无法插入

转载 作者:行者123 更新时间:2023-12-02 11:23:59 25 4
gpt4 key购买 nike

亲爱的开发者。

我要问你一个我认为很难解决的问题,因为我不知道回答的基本原因, 我的意思是说??让我们来看看。 JBoss文档中的关联没有得到全面的答案:为什么我应该使用JoinTable而不是外键并且我不完全理解映射是如何工作的, 我这是什么意思?我知道什么是关联,是多对多或多对一等,以及它们的目标是什么,但是它们如何工作以及如何相互协作不需要有关双向或单向或 joinTable 或关联的答案,我想要一个可以找到有关我的两个问题的完整信息的链接:

1) 为什么我应该使用JoinTable而不是外键????

2)实体如何工作和相互协作(没有解释什么是多对多等关联以及双向或单向关联)???

所以,我有一段代码因为误解而陷入困境,我只是想插入数据我是 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>

  • 错误:

INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [app-context.xml] INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning INFO : org.springframework.context.support.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@c93274: startup date [Thu May 09 11:38:23 EDT 2013]; root of context hierarchy INFO : org.springframework.beans.factory.config.PropertyPlaceholderConfigurer - Loading properties file from class path resource [connection.properties] INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b9c2f0: defining beans [dataSource,transactionManager,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,daoInsertDataInterface,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,sessionFactory,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor];

root of factory hierarchy Hibernate: insert into TYPE (TYPE_NAME, VERSION) values (?, ?) WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 1048, SQLState: 23000 ERROR: org.hibernate.util.JDBCExceptionReporter - Column 'TYPE_NAME' cannot be null Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [edu.demidov.dom.Type] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2327) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2834) at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) at 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) 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.engine.CascadingAction$5.cascade(CascadingAction.java:252) at org.hibernate.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) at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:450) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:282) 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) 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) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96) at 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) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at $Proxy22.insertData(Unknown Source) at edu.demidov.dao.AppTEst.main(AppTEst.java:22) Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'TYPE_NAME' cannot be null at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) 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:1041) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458) at com.mysql.jdbc.PreparedStatement.executeUpdate(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 more SS

谢谢你们,伙计们。

最佳答案

Why I should use JoinTable insted of Foreign Key?

你不应该。你有选择的权利。如果您更喜欢使用连接表,请使用连接表。如果您更喜欢使用连接列(子表中的外键),请使用连接列。如果您已有无法更改的架构,请选择与现有架构对应的选项。

how entity work's and collaborate with each other

我不确定你的意思。如果实体 School 与实体 Teacher 具有 OneToMany 关联,当您从 session 中获取学校并询问其教师时,Hibernate 将从数据库中为您加载它们。如果您将一名教师添加到学校的教师集合中,Hibernate 将为您填充连接列或连接表。目标只是像操作存储在内存中的简单对象一样操作对象,并让 Hibernate 加载它们并将它们保存到数据库中。

现在关于异常和您问题的标题,异常消息清楚地说明了这一点:

insert into TYPE (TYPE_NAME, VERSION) values (?, ?) WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 1048, SQLState: 23000 ERROR: org.hibernate.util.JDBCExceptionReporter - Column 'TYPE_NAME' cannot be null

您正在尝试插入一个类型为 Type 的实体(这个名字选得多么糟糕!),但名称为空。并且数据库列不接受 null。所以你会得到这个异常。如果 null 是有效的类型名称,请更改表定义,或者修复您的代码以确保您不会尝试插入 null 名称。

此外,请遵守 Java 命名约定。你的代码真的很难读。

关于java - 列不能为空无法插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16462267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com