gpt4 book ai didi

java.sql.BatchUpdateException : Unknown column 'xxx' in 'field list'

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:13 58 4
gpt4 key购买 nike

我正在尝试使用额外的列保存多对多关系,但我的代码运行不正常。我希望有人能帮助我

我的数据库是:

CREATE TABLE MUSICISTA (
ID INTEGER UNSIGNED NOT NULL,
NOME VARCHAR(155) NOT NULL,
COGNOME VARCHAR(155) NOT NULL,
DATA_NASCITA DATE NULL,
PRIMARY KEY(ID)
);

CREATE TABLE BAND_has_MUSICISTA (
BAND_ID INTEGER UNSIGNED NOT NULL,
MUSICISTA_ID INTEGER UNSIGNED NOT NULL,
BAND_ACCEPT BOOL NOT NULL,
MUSICISTA_ACCEPT BOOL NOT NULL,
PRIMARY KEY(BAND_ID, MUSICISTA_ID)
);

CREATE TABLE BAND (
ID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
VIDEO_ID INTEGER UNSIGNED NULL,
AUDIO_ID INTEGER UNSIGNED NULL,
CITTA_ID INTEGER UNSIGNED NULL,
NOME VARCHAR(155) NOT NULL,
DESCRIZIONE VARCHAR(155) NOT NULL,
PRIMARY KEY(ID)
);

ALTER TABLE BAND_has_MUSICISTA
ADD CONSTRAINT TO_BAND
FOREIGN KEY (BAND_ID)
REFERENCES BAND (ID);

ALTER TABLE BAND_has_MUSICISTA
ADD CONSTRAINT TO_MUSICISTA
FOREIGN KEY (MUSICISTA_ID)
REFERENCES MUSICISTA (ID);

我的实体是:

音乐家.java

package it.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;


/**
* The persistent class for the MUSICISTA database table.
*
*/
@Entity
@Table(name="MUSICISTA")
@NamedQuery(name="Musicista.findAll", query="SELECT m FROM Musicista m")
public class Musicista implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(generator="SharedPrimaryKeyGenerator")
@GenericGenerator(name="SharedPrimaryKeyGenerator", strategy="foreign", parameters = @Parameter(name="property", value="account"))
@Column(name = "ID", unique = true, nullable = false)
private int id;

@Column(nullable=false, length=155)
private String cognome;

@Temporal(TemporalType.DATE)
@Column(name="DATA_NASCITA")
private Date dataNascita;

@Column(nullable=false, length=155)
private String nome;

//bi-directional one-to-one association to Account
@OneToOne
@PrimaryKeyJoinColumn
private Account account;

@OneToMany(mappedBy="pk.musicista", fetch = FetchType.LAZY, cascade=CascadeType.ALL)
private List<BandHasMusicista> bandHasMusicista;

public Musicista() {
}

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

public String getCognome() {
return this.cognome;
}

public void setCognome(String cognome) {
this.cognome = cognome;
}

public Date getDataNascita() {
return this.dataNascita;
}

public void setDataNascita(Date dataNascita) {
this.dataNascita = dataNascita;
}

public String getNome() {
return this.nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public Account getAccount() {
return this.account;
}

public void setAccount(Account account) {
this.account = account;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.musicista")
public List<BandHasMusicista> getBandHasMusicista() {
return bandHasMusicista;
}

public void setBandHasMusicista(List<BandHasMusicista> bandHasMusicista) {
this.bandHasMusicista = bandHasMusicista;
}

}

BandHasMusicista.java

package it.entities;

import java.io.Serializable;

import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;


/**
* The persistent class for the band_has_musicista database table.
*
*/
@Entity
@Table(name="BAND_HAS_MUSICISTA")
@AssociationOverrides({
@AssociationOverride(name = "pk.musicista",
joinColumns = @JoinColumn(name = "MUSICISTA_ID")),
@AssociationOverride(name = "pk.band",
joinColumns = @JoinColumn(name = "BAND_ID")) })
@NamedQuery(name="BandHasMusicista.findAll", query="SELECT b FROM BandHasMusicista b")
public class BandHasMusicista implements Serializable {
private static final long serialVersionUID = 1L;

private BandHasMusicistaPK pk = new BandHasMusicistaPK();

@Column(name="BAND_ACCEPT", nullable=false)
private boolean bandAccept;

@Column(name="MUSICISTA_ACCEPT", nullable=false)
private boolean musicistaAccept;

private Band band;

private Musicista musicista;

public BandHasMusicista() {
}

@EmbeddedId
public BandHasMusicistaPK getPk() {
return pk;
}

public void setPk(BandHasMusicistaPK pk) {
this.pk = pk;
}

public boolean getBandAccept() {
return this.bandAccept;
}

public void setBandAccept(boolean bandAccept) {
this.bandAccept = bandAccept;
}

public boolean getMusicistaAccept() {
return this.musicistaAccept;
}

public void setMusicistaAccept(boolean musicistaAccept) {
this.musicistaAccept = musicistaAccept;
}

@Transient
public Band getBand() {
return this.band;
}

public void setBand(Band band) {
this.band = band;
}

@Transient
public Musicista getMusicista() {
return this.musicista;
}

public void setMusicista(Musicista musicista) {
this.musicista = musicista;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((band == null) ? 0 : band.hashCode());
result = prime * result + (bandAccept ? 1231 : 1237);
result = prime * result
+ ((musicista == null) ? 0 : musicista.hashCode());
result = prime * result + (musicistaAccept ? 1231 : 1237);
result = prime * result + ((pk == null) ? 0 : pk.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BandHasMusicista other = (BandHasMusicista) obj;
if (band == null) {
if (other.band != null)
return false;
} else if (!band.equals(other.band))
return false;
if (bandAccept != other.bandAccept)
return false;
if (musicista == null) {
if (other.musicista != null)
return false;
} else if (!musicista.equals(other.musicista))
return false;
if (musicistaAccept != other.musicistaAccept)
return false;
if (pk == null) {
if (other.pk != null)
return false;
} else if (!pk.equals(other.pk))
return false;
return true;
}

}

BandHasMusicistaPK.java

package it.entities;

import java.io.Serializable;

import javax.persistence.*;

/**
* The primary key class for the band_has_musicista database table.
*
*/
@Embeddable
public class BandHasMusicistaPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;

@ManyToOne
private Band band;

@ManyToOne
private Musicista musicista;

public BandHasMusicistaPK() {
}
public Band getBand() {
return this.band;
}
public void setBand(Band band) {
this.band = band;
}
public Musicista getMusicista() {
return this.musicista;
}
public void setMusicista(Musicista musicista) {
this.musicista = musicista;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((band == null) ? 0 : band.hashCode());
result = prime * result
+ ((musicista == null) ? 0 : musicista.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BandHasMusicistaPK other = (BandHasMusicistaPK) obj;
if (band == null) {
if (other.band != null)
return false;
} else if (!band.equals(other.band))
return false;
if (musicista == null) {
if (other.musicista != null)
return false;
} else if (!musicista.equals(other.musicista))
return false;
return true;
}

}

乐队.java

package it.entities;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;


/**
* The persistent class for the band database table.
*
*/
@Entity
@Table(name="BAND")
@NamedQuery(name="Band.findAll", query="SELECT b FROM Band b")
public class Band implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private int id;

private String descrizione;

private String nome;

//bi-directional many-to-one association to Audio
@ManyToOne
private Audio audio;

//bi-directional many-to-one association to Video
@ManyToOne
private Video video;

//bi-directional many-to-one association to Citta
@ManyToOne
private Citta citta;

@OneToMany(mappedBy="pk.band", fetch = FetchType.LAZY, cascade=CascadeType.ALL)
private List<BandHasMusicista> bandHasMusicista;

public Band() {
}

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

public String getDescrizione() {
return this.descrizione;
}

public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}

public String getNome() {
return this.nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public Audio getAudio() {
return this.audio;
}

public void setAudio(Audio audio) {
this.audio = audio;
}

public Video getVideo() {
return this.video;
}

public void setVideo(Video video) {
this.video = video;
}

public Citta getCitta() {
return this.citta;
}

public void setCitta(Citta citta) {
this.citta = citta;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.band")
public List<BandHasMusicista> getBandHasMusicista() {
return bandHasMusicista;
}

public void setBandHasMusicista(List<BandHasMusicista> bandHasMusicista) {
this.bandHasMusicista = bandHasMusicista;
}

}

BandLogic.java

package it.logic;

import it.entities.Band;
import it.entities.BandHasMusicista;
import it.entities.Citta;
import it.entities.Musicista;
import it.interfaces.ServiceBean;
import it.invo.AddBandInVO;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

public class BandLogic {

EntityManager em = null;

/**
* Default constructor.
*/
public BandLogic() {
em = ServiceBean.getEm();
}

public boolean addBand(AddBandInVO inVO) {
System.out.println("IN - addBand() INPUT = " + inVO);

Musicista musicista = em.find(Musicista.class, inVO.getAccountId());

Citta citta = em.find(Citta.class, inVO.getCitta());

if(musicista != null){

Band band = new Band();
band.setNome(inVO.getNome());
band.setDescrizione(inVO.getDescrizione());
band.setCitta(citta);
band.setBandHasMusicista(new ArrayList<BandHasMusicista>());

BandHasMusicista bandHasMusicista = new BandHasMusicista();
bandHasMusicista.setBand(band);
bandHasMusicista.setMusicista(musicista);
bandHasMusicista.setBandAccept(true);
bandHasMusicista.setMusicistaAccept(true);

List<BandHasMusicista> bandMusicista = band.getBandHasMusicista();
bandMusicista.add(bandHasMusicista);

em.persist(band);
em.flush();
em.clear();
}else{
System.out.println("Account is not Musicista");
return false;
}

System.out.println("OUT - addBand()");
return true;
}

}

stackTrace 是:

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:307)
at it.logic.BandLogic.addBand(BandLogic.java:51)
at it.interfaces.ServiceBean.addBand(ServiceBean.java:184)
at it.test.BandTest.testAddBand(BandTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:304)
... 22 more
Caused by: java.sql.BatchUpdateException: Unknown column 'bandAccept' in 'field list'
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2054)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1467)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 28 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'bandAccept' in 'field list'
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:4187)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
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.executeBatchSerially(PreparedStatement.java:2006)
... 31 more

最佳答案

仔细检查相关代码:

@Entity
@Table(name="BAND_HAS_MUSICISTA")
@AssociationOverrides({
@AssociationOverride(name = "pk.musicista",
joinColumns = @JoinColumn(name = "MUSICISTA_ID")),
@AssociationOverride(name = "pk.band",
joinColumns = @JoinColumn(name = "BAND_ID")) })
@NamedQuery(name="BandHasMusicista.findAll", query="SELECT b FROM BandHasMusicista b")
public class BandHasMusicista implements Serializable {
private static final long serialVersionUID = 1L;

private BandHasMusicistaPK pk = new BandHasMusicistaPK();

@Column(name="BAND_ACCEPT", nullable=false)
private boolean bandAccept;

我认为在数据库中你有带或不带下划线的列。还要记录 sql 查询以确保执行了您想要的内容。

关于java.sql.BatchUpdateException : Unknown column 'xxx' in 'field list' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18175166/

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