您好我的朋友,我希望您可以帮忙修复此错误。我为时尚mangamnet系统做一个项目,我有java持久性的错误,说我有无效的列,我认为该列是相同的类型IDSHTETI_ID_Shteti是LIKE ID_SHTETI但不存在于我生成的类中,从实体到类制造商,我做了java persistence,你能帮帮我吗?
错误:
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'IDSHTETI_ID_Shteti'.
Error Code: 207
Call: INSERT INTO Kompania (Emri, Kodi_Postar, Qyteti, Rruga, IDSHTETI_ID_Shteti, ID_Shteti, ID_Mengajeri) VALUES (?, ?, ?, ?, ?, ?, ?)
bind => [7 parameters bound]
Query: InsertObjectQuery(BL.Kompania[ iDKompania=null ])
类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package BL;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
*
* @author dorron
*/
@Entity
@Table(name = "Kompania")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Kompania.findAll", query = "SELECT k FROM Kompania k")
, @NamedQuery(name = "Kompania.findByIDKompania", query = "SELECT k FROM Kompania k WHERE k.iDKompania = :iDKompania")
, @NamedQuery(name = "Kompania.findByEmri", query = "SELECT k FROM Kompania k WHERE k.emri = :emri")
, @NamedQuery(name = "Kompania.findByQyteti", query = "SELECT k FROM Kompania k WHERE k.qyteti = :qyteti")
, @NamedQuery(name = "Kompania.findByRruga", query = "SELECT k FROM Kompania k WHERE k.rruga = :rruga")
, @NamedQuery(name = "Kompania.findByKodiPostar", query = "SELECT k FROM Kompania k WHERE k.kodiPostar = :kodiPostar")})
public class Kompania implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "ID_Kompania")
@GeneratedValue(generator="InvSeq")
@SequenceGenerator(name="InvSeq",sequenceName="INV_SEQ", allocationSize=1)
private Integer iDKompania;
@Basic(optional = false)
@Column(name = "Emri")
private String emri;
@Basic(optional = false)
@Column(name = "Qyteti")
private String qyteti;
@Basic(optional = false)
@Column(name = "Rruga")
private String rruga;
@Basic(optional = false)
@Column(name = "Kodi_Postar")
private int kodiPostar;
@OneToMany(mappedBy = "iDKompania")
private Collection<Stafi> stafiCollection;
@JoinColumns({
@JoinColumn(name = "ID_Mengajeri", referencedColumnName = "ID_Mengajeri"),
@JoinColumn(name = "ID_Shteti", referencedColumnName = "ID_Shteti")
})
@OneToOne
private Mengajeri iDMengajeri;
@ManyToOne(optional = false)
private Shteti iDShteti;
@OneToMany(mappedBy = "iDKompania")
private Collection<Blerjet> blerjetCollection;
@OneToMany(mappedBy = "iDKompania")
private Collection<Stoku> stokuCollection;
@OneToMany(mappedBy = "iDKompania")
private Collection<Faktura> fakturaCollection;
@OneToMany(mappedBy = "iDKompania")
private Collection<KontaktiKompania> kontaktiKompaniaCollection;
public Kompania() {
}
public Kompania(Integer iDKompania) {
this.iDKompania = iDKompania;
}
public Kompania(Integer iDKompania, String emri, String qyteti, String rruga, int kodiPostar) {
this.iDKompania = iDKompania;
this.emri = emri;
this.qyteti = qyteti;
this.rruga = rruga;
this.kodiPostar = kodiPostar;
}
public Kompania( String emri, String qyteti, String rruga, int kodiPostar , Shteti shteti) {
this.emri = emri;
this.qyteti = qyteti;
this.rruga = rruga;
this.kodiPostar = kodiPostar;
this.iDShteti = shteti;
}
public Integer getIDKompania() {
return iDKompania;
}
public void setIDKompania(Integer iDKompania) {
this.iDKompania = iDKompania;
}
public String getEmri() {
return emri;
}
public void setEmri(String emri) {
this.emri = emri;
}
public String getQyteti() {
return qyteti;
}
public void setQyteti(String qyteti) {
this.qyteti = qyteti;
}
public String getRruga() {
return rruga;
}
public void setRruga(String rruga) {
this.rruga = rruga;
}
public int getKodiPostar() {
return kodiPostar;
}
public void setKodiPostar(int kodiPostar) {
this.kodiPostar = kodiPostar;
}
@XmlTransient
public Collection<Stafi> getStafiCollection() {
return stafiCollection;
}
public void setStafiCollection(Collection<Stafi> stafiCollection) {
this.stafiCollection = stafiCollection;
}
public Mengajeri getIDMengajeri() {
return iDMengajeri;
}
public void setIDMengajeri(Mengajeri iDMengajeri) {
this.iDMengajeri = iDMengajeri;
}
public Shteti getIDShteti() {
return iDShteti;
}
public void setIDShteti(Shteti iDShteti) {
this.iDShteti = iDShteti;
}
@XmlTransient
public Collection<Blerjet> getBlerjetCollection() {
return blerjetCollection;
}
public void setBlerjetCollection(Collection<Blerjet> blerjetCollection) {
this.blerjetCollection = blerjetCollection;
}
@XmlTransient
public Collection<Stoku> getStokuCollection() {
return stokuCollection;
}
public void setStokuCollection(Collection<Stoku> stokuCollection) {
this.stokuCollection = stokuCollection;
}
@XmlTransient
public Collection<Faktura> getFakturaCollection() {
return fakturaCollection;
}
public void setFakturaCollection(Collection<Faktura> fakturaCollection) {
this.fakturaCollection = fakturaCollection;
}
@XmlTransient
public Collection<KontaktiKompania> getKontaktiKompaniaCollection() {
return kontaktiKompaniaCollection;
}
public void setKontaktiKompaniaCollection(Collection<KontaktiKompania> kontaktiKompaniaCollection) {
this.kontaktiKompaniaCollection = kontaktiKompaniaCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (iDKompania != null ? iDKompania.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Kompania)) {
return false;
}
Kompania other = (Kompania) object;
if ((this.iDKompania == null && other.iDKompania != null) || (this.iDKompania != null && !this.iDKompania.equals(other.iDKompania))) {
return false;
}
return true;
}
@Override
public String toString() {
return "BL.Kompania[ iDKompania=" + iDKompania + " ]";
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package GUI;
import BL.Kompania;
import BL.Mengajeri;
import DAL.KompaniaRepository;
import DAL.KompaniaException;
import BL.Shteti;
import DAL.ShtetiRepository;
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
* @author dorron
*/
public class RegjistrimiKompaniseWindow extends javax.swing.JInternalFrame {
/**
* Creates new form RegjistrimiKompaniseWindow
*/
public RegjistrimiKompaniseWindow() {
initComponents();
}
//Objektet e incializuara
KompaniaRepository kR = new KompaniaRepository();
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
emriTextField = new javax.swing.JTextField();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
shtetiComboBox = new javax.swing.JComboBox<>();
jLabel2 = new javax.swing.JLabel();
qytetiTextField = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
rrugaTextField = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
kPostarTextField = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setClosable(true);
setIconifiable(true);
setTitle("Regjistrimi i Kompanisë");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Emri"));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(emriTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(emriTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(26, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Adresa"));
jLabel1.setText("Shteti:");
shtetiComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new ShtetiRepository().findAll().toArray()));
shtetiComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
shtetiComboBoxActionPerformed(evt);
}
});
jLabel2.setText("Qyteti:");
jLabel3.setText("Rruga:");
jLabel4.setText("Kodi Postar:");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(kPostarTextField))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(rrugaTextField))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(shtetiComboBox, 0, 165, Short.MAX_VALUE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(qytetiTextField)))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(shtetiComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(qytetiTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(rrugaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(kPostarTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(20, Short.MAX_VALUE))
);
jButton1.setText("Ruaj");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(80, 80, 80)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(70, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addContainerGap(18, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void shtetiComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
if (shtetiComboBox.getSelectedIndex() == -1
&& emriTextField.getText().equals("") || emriTextField == null
&& kPostarTextField.getText().equals("") || kPostarTextField == null
&& qytetiTextField.getText().equals("") || qytetiTextField == null
&& rrugaTextField.getText().equals("") || rrugaTextField == null) {
JOptionPane.showMessageDialog(this, "Nuk e ke mbushur asnje opsion!", "Gabim!", JOptionPane.ERROR_MESSAGE);
} else if (shtetiComboBox.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(this, "Nuk e ke zgjedhur shtetin!", "Gabim!", JOptionPane.ERROR_MESSAGE);
} else if (emriTextField.getText().equals("") || emriTextField == null) {
JOptionPane.showMessageDialog(this, "Nuk e ke shkruar emrin!", "Gabim!", JOptionPane.ERROR_MESSAGE);
} else if (qytetiTextField.getText().equals("") || qytetiTextField == null) {
JOptionPane.showMessageDialog(this, "Nuk e ke shkruar qytetin!", "Gabim!", JOptionPane.ERROR_MESSAGE);
} else if(kPostarTextField.getText().equals("") || kPostarTextField == null){
JOptionPane.showMessageDialog(this, "Nuk e ke shkruar kodin postar!", "Gabim!", JOptionPane.ERROR_MESSAGE);
} else if(rrugaTextField.getText().equals("") || rrugaTextField == null){
JOptionPane.showMessageDialog(this, "Nuk e ke shkruar kodin rrugen!", "Gabim!", JOptionPane.ERROR_MESSAGE);
} else{
String emri = emriTextField.getText();
Shteti sh = (Shteti)shtetiComboBox.getSelectedItem();
String qyteti = qytetiTextField.getText();
String rruga = rrugaTextField.getText();
int kPostar = Integer.parseInt(kPostarTextField.getText());
Mengajeri m = null;
Kompania k = new Kompania(emri, qyteti, rruga, kPostar,sh);
Random rand = new Random();
// int nr = rand.nextInt(9999)+1;
// k.setIDKompania(nr);
kR.create(k);
}
} catch (KompaniaException ke) {
JOptionPane.showMessageDialog(this, ke.getMessage(), "Gabim!", JOptionPane.ERROR_MESSAGE);
}
}
// Variables declaration - do not modify
private javax.swing.JTextField emriTextField;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField kPostarTextField;
private javax.swing.JTextField qytetiTextField;
private javax.swing.JTextField rrugaTextField;
private javax.swing.JComboBox<Object> shtetiComboBox;
// End of variables declaration
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package DAL;
import BL.Kompania;
import java.util.List;
import javax.persistence.NoResultException;
import javax.persistence.Query;
/**
*
* @author dorron
*/
public class KompaniaRepository extends EntMngClass implements KompaniaInterface{
/**
* Kjo ben ruajten e Kompanise ne databaze ne tabelen
* Kompania , kthen Kompania Exception nese ekzsiton apo
* ka ndonje gabim ne ruajten apo ne konektim.
* @param k
* @throws KompaniaException
*/
@Override
public void create(Kompania k) throws KompaniaException{
try{
em.getTransaction().begin();
Kompania kk = new Kompania();
k.setEmri(k.getEmri());
k.setIDShteti(k.getIDShteti());
k.setKodiPostar(k.getKodiPostar());
k.setRruga(k.getRruga());
em.persist(kk);
em.getTransaction().commit();
}catch(Throwable thro){
if(thro.getMessage().contains("2627")){
throw new KompaniaException("Create Error: Kompania Ekzsiton!");
}else{
throw new KompaniaException("Create Error: "+thro.getClass()+" - "+thro.getMessage()+"!");
}
}
}
/**
* Kjo ben editimin e kompanise ne databaze ne tabelen
* Kompania, kthen Kompania Exception nese ekzsiton apo
* ka ndonje gabim ne ruajten apo ne konektim.
* @param k
* @throws KompaniaException
*/
@Override
public void edit(Kompania k) throws KompaniaException{
try{
em.getTransaction().begin();
em.merge(k);
em.getTransaction().commit();
}catch(Throwable thro){
if(thro.getMessage().contains("2627")){
throw new KompaniaException("Edit Error: Kompania Ekzsiton!");
}else{
throw new KompaniaException("Edit Error: "+thro.getClass()+" - "+thro.getMessage()+"!");
}
}
}
/**
* Kjo ben fshrijen e kompanise ne databaze ne tabelen
* Kompania , kthen Kompania Exception nese e dhena eshte
* ne perdorim apo ka ndonje gabim ne lidhje.
* @param k
* @throws KompaniaException
*/
@Override
public void remove(Kompania k) throws KompaniaException{
try{
em.getTransaction().begin();
em.remove(k);
em.getTransaction().commit();
}catch(Throwable thro){
if(thro.getMessage().contains("547")){
throw new KompaniaException("Remove Error: Te dhenat e kompanise jane ne perdorim!");
}else{
throw new KompaniaException("Remove Error: "+thro.getClass()+" - "+thro.getMessage());
}
}
}
/**
* Kjo ben kthimit e krejt Kompanit ne form te listes
* @return
*/
@Override
public List<Kompania> findAll(){
return em.createNamedQuery("Kompania.findAll").getResultList();
}
/**
* Kjo ben gjeten kompanise permes parametrit te ids kompanise
* @param i
* @return
* @throws KompaniaException
*/
@Override
public Kompania findById(int i) throws KompaniaException{
Query query = em.createQuery("SELECT k FROM Kompania k WHERE k.iDKompania = :id");
query.setParameter("id", i);
try{
return (Kompania)query.getSingleResult();
}catch(NoResultException nre){
throw new KompaniaException("Nuk ekzsiton!");
}
}
}
表:
CREATE TABLE Kompania(
ID_Kompania int primary key identity(1,1),
Emri varchar(50) not null,
ID_Shteti int foreign key references Shteti(ID_Shteti) not null,
Qyteti varchar(50) not null,
Rruga varchar(50) not null,
Kodi_Postar int not null
);
Alter table Kompania
add ID_Mengajeri int unique foreign key references Mengajeri(ID_Mengajeri);