- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用额外的列保存多对多关系,但我的代码运行不正常。我希望有人能帮助我
我的数据库是:
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/
下面的代码给出了一个BatchUpdateException,我不知道为什么。 public void createContactmoment(Document document) { try
当我由于违反唯一约束而出现 BatchUpdateException 时,是否有办法确定批插入中的哪条记录违反了规定?例如,假设我正在通过调用 PreparedStatement.executeBat
我有一个 java 程序,可以将文本文件中的数千行数据读取到数据库中。它使用语句批处理一次插入几千行。 如果任何行无法更新,我将继续尝试其余行,直到列表末尾。之后,我返回并检查哪些行失败,然后将这些行
这个问题可能很愚蠢,但我面临着这种情况: 我有一个每次用户发送请求时都会填充的表,我在插入的最后一行中的一个字段(日期)上使用日期。问题是,我插入的最后一行总是会尝试插入自身,因为它在我正在考虑的范围
我正在使用批处理在数据库中插入大量记录。如果其中任何一个失败,我只会在更新计数数组中获得记录号(和最后一条异常消息)。 是否有可能得到所有错误的原因? 我正在使用 Java7 和 Mysql 连接器
我从 stmt.executeBatch() 语句中得到这个 BatchUpdateException: BatchUpdateException: A resultset was created f
我有许多映射数据流在过去几个月中一直定期运行,其中一些从昨天开始出现故障。 数据流模式是 - Source: 2 Azure SQL DB tables, a lookup table in Syna
我对 Hybris 非常陌生,我正在开发 6.2 版本。经过一些尝试后,我决定创建一个新的空扩展来工作。因此,第一步是创建一个新文件夹,在其中导入平台的所有“准备好的”B2C 扩展。这样做之后我导入了
我正在使用 Postgres 9.3、Spring 和 Java。 对于来自 javadoc 的 BatchUpdateException: After a command in a batch up
我编写了一个 java 程序,它使用 jdbc 连接到 mysql 数据库。它将记录插入到 mysql。sql是这样的: INSERT INTO my_schema.my_table VALUE
我尝试使用 PreparedStatement.executeBatch() 执行批处理查询,并 try catch BatchUpdateException 中发生的任何异常。但我在调用 Batch
我有一个类似 的域 class BusinessOrganization { static hasMany = [organizationBusinessTypes:OrganizationB
我正在使用具有一对一关系的 Hibernate XML 映射构建一个应用程序。我的实体是 Auto 和 Targa。 这是我的 Java 类定义。 Targa.java public class Ta
我遇到了一些麻烦。 我正在尝试列出一些有关在 executeUpdate() 期间失败的查询的数据,并且我读到可以捕获 BatchUpdateException 然后获取 updateCount,它会
我有一个批量更新函数,不包含任何 GROUP_CONCAT() 调用; UPDATE product SET viewed = ? WHERE product_id = ? 如
一些遗留代码表现出奇怪的行为。我有一个 Hibernate 标准: Session session = em.unwrap(Session.class); Criteria crit = sessio
spark2.2 中使用 jdbc 驱动写入 TiDB 时出现此错误: java.sql.BatchUpdateException: transaction too large, len:300200
我正在尝试使用额外的列保存多对多关系,但我的代码运行不正常。我希望有人能帮助我 我的数据库是: CREATE TABLE MUSICISTA ( ID INTEGER UNSIGNED NOT N
我正在尝试使用 JdbcTemplate 将当前时间戳存储在 Oracle 数据库中。我试图在其中存储两列 timestamp ,但一列是 VARCHAR ,一列是 TIMESTAMP ,但是查看异常
我正在尝试从 MATLAB 写入 PostgreSQL 数据库表。我已经使用 JDBC 建立了连接并创建了表,但是当我尝试插入记录时出现 BatchUpdateException。 插入数据的 MAT
我是一名优秀的程序员,十分优秀!