hibernate one to many annotation无效的列名

时间:2016-07-19 09:58:20

标签: java hibernate spring-mvc

我有2个表:人员和帐户(一对多关系)。

在我的Person课程中,我有:

@Entity
@Table(name="persons")
public class Person {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    private String username;
    private String password;
    private String country;
    private boolean admin;

    @OneToMany(mappedBy="person")
    @Cascade({CascadeType.ALL})
    private Set<Account> accounts;

    public Person(String name, String username, String password, String country, boolean admin) {
        this.name = name;
        this.username = username;
        this.password = password;
        this.country = country;
        this.admin = admin;
    }

    public Person(){ }

    //getters and setters
}

在我的帐户课程中,我有:

@Entity
@Table(name="accounts")
public class Account {

    @Id
    @Column(name="accountId")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int accountId;
    private int balance;
    private String currency;

    @ManyToOne
    @JoinColumn(name="id")
    private Person person;

    public Account(int balance, String currency) {
        this.balance = balance;
        this.currency = currency;
    }

    private Account(){ }

    //getters and setters
}

我使用SQL服务器,并使用以下代码创建表:

CREATE TABLE persons(
    id int IDENTITY(1,1) PRIMARY KEY NOT NULL,
    name nvarchar(30) NOT NULL,
    username nvarchar(30) NOT NULL,
    password nvarchar(30) NOT NULL,
    country nvarchar(30) NOT NULL,
    admin bit NOT NULL
);

CREATE TABLE accounts(
    accountId int IDENTITY(10000,1) PRIMARY KEY NOT NULL,
    balance int NOT NULL,
    currency nvarchar(5) NOT NULL,
    userId int NOT NULL foreign key references persons(id)
);

在我的控制器中,我保存了一个人并且它有效,然后当我尝试保存该人的2个帐户时,我收到以下错误:java.sql.SQLException: Invalid column name 'id'.代码:

    Person person = new Person("Maurice","vvvvv","ccccc", "France", true);
    this.personService.addPerson(person);
    Account account1 = new Account(1000, "eur");
    Account account2 = new Account(700, "gbp");
    account1.setPerson(person);
    account2.setPerson(person);
    this.accountService.addAccount(account1);
    this.accountService.addAccount(account2);

这不是我第一次使用一对多的关系,但我之前没有遇到过此错误。

完整的堆栈跟踪:

  

SEVERE:servlet [appServlet]的Servlet.service()与上下文有关   path [/ BankingApp]抛出异常[请求处理失败;嵌套   异常是org.hibernate.exception.SQLGrammarException:不能   具有根本原因java.sql.SQLException的执行语句:无效   列名称&#39; id&#39;。在   net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)     at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988)     在net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421)at   net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:671)at at   net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:613)     在   net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:572)     在   net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:727)     在   org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)     在   org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)     在   org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)     在   org.hibernate.id.IdentityGenerator $ GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)     在   org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)     在   org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)     在   org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)     在   org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)     在org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:492)     在   org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:197)     在   org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)     在   org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:216)     在   org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:334)     在   org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:289)     在   org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:195)     在   org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)     在   org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)     在   org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)     在   org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)     在   org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)     在org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)     在org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)     在com.haynes.dao.AccountDAOImpl.addAccount(AccountDAOImpl.java:26)     在   com.haynes.services.AccountServiceImpl.addAccount(AccountServiceImpl.java:21)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)     在   org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99)     在   org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)     在   org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)     在   org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)     在com.sun.proxy。$ Proxy135.addAccount(未知来源)at   com.haynes.controllers.Controller.addPerson(Controller.java:47)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)     在   org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)     在   org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)     在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)     在   org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)     在   org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)     在   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)     在   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)     在   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)     在   org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:622)at   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     在   org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)     在   org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)     在   org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:670)     在   org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun(AprEndpoint.java:2508)     在   org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.run(AprEndpoint.java:2497)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(未知来源)

2 个答案:

答案 0 :(得分:1)

@JoinColumn(name="id")应为@JoinColumn(name="userId")

因为userId在帐户表

中用作外键

答案 1 :(得分:0)

看起来好像Person.java或Account.java的id列字段在数据库中存储为Id,而在您的实体中,您将其命名为id。

您能否分享Person.java类和错误的完整堆栈跟踪,请同时检查您的数据库以确保字段正确

感谢。