昨天有一個問題,我無法連接到derby數據庫(很可能是由於persistence.xml問題)我已經被引導去修改我的persistence.xml以便它包含兩個額外的屬性;EJB項目沒有連接到正確的Derby數據庫/表
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
已經加入他們,我沒有得到面對一堆錯誤,如SCHEMA "xx" doesn't exist
或Unknown entity bean class: class model.Userbay, please verify that this class has been marked with the @Entity annotation.
擁有的一切,雖然現在將被罰款,我意識到這是不是這麼回事..起初我試圖通過Entity Manager的.find()方法從數據庫中獲取其中一行。經過一段時間的測試以從數據庫中檢索記錄,我認爲嘗試將某些東西插入數據庫並查看會發生什麼可能會更好。執行了下面這行代碼;
emgr.createNativeQuery("insert into ADRIAN.USERBAY (USER_NAME, PASSWORD, EMAIL, FIRST_NAME, LAST_NAME) values('testts123s', '1', '1', '1', '1')").executeUpdate();
我注意到,沒有被插入到數據庫中。不過有試圖執行.find()找到我剛插入(testts123s)主鍵已經找到了記錄(雖然在數據源資源管理器數據庫的表沒有填充此記錄)。因此,我的問題是發生了什麼,我被連接到一個空表?
以下是代碼;
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="EJBAuctionv2">
<!-- <jta-data-source>JDBC/MyDB</jta-data-source> -->
<class>model.Userbay</class>
<class>model.Item</class>
<class>model.Category</class>
<properties>
<property name="javax.persistence.jdbc.password" value="123" />
<property name="javax.persistence.jdbc.user" value="adrian" />
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeededDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:C:\Users\Adrian\MyDB;create=true" />
<!-- <property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" /> -->
</properties>
</persistence-unit>
</persistence>
放在userRegistration SessionBean的
package auction;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Remote;
import javax.ejb.Singleton;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.management.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import model.Userbay;
/**
* Session Bean implementation class userRegistrationSB
*/
@Remote @Stateless
public class userRegistrationSB implements userRegistrationSBRemote {
//@EJB private Userbay user;
@PersistenceContext(name = "EJBAuctionv2") private EntityManager emgr;
/**
* Default constructor.
*/
public userRegistrationSB() {}
@Override
public boolean registerUser(String username, String password, String email,
String firstname, String lastname) {
boolean registered = false;
//emgr.createNativeQuery("insert into ADRIAN.USERBAY (USER_NAME, PASSWORD, EMAIL, FIRST_NAME, LAST_NAME) values('testts123s', '1', '1', '1', '1')").executeUpdate();
System.out.println("Registering an user with username: " + username);
Userbay user = emgr.find(model.Userbay.class, username);
if (user == null) {
System.out.println("Username doesn't exist.");
registered = true;
} else {
registered = false;
System.out.println("Username already exists.");
}
return registered;
}
@Override
public boolean userExists(String username) {
return false;
}
@Override
public boolean userMatchesPassword(String username, String password) {
return false;
}
}
Userbay實體Bean
package model;
import java.io.Serializable;
import javax.persistence.*;
import java.util.List;
@Entity @Table (name = "Userbay")
@NamedQuery(name="Userbay.findAll", query="SELECT u FROM Userbay u")
public class Userbay implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue (strategy = GenerationType.TABLE)
@Column(name="USER_NAME")
private String userName;
private String email;
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="LAST_NAME")
private String lastName;
@Column(name="PASSWORD")
private String password;
//bi-directional many-to-one association to Item
@OneToMany(mappedBy="userbay")
private List<Item> items;
public Userbay() {
}
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Item> getItems() {
return this.items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public Item addItem(Item item) {
getItems().add(item);
item.setUserbay(this);
return item;
}
public Item removeItem(Item item) {
getItems().remove(item);
item.setUserbay(null);
return item;
}
}
你有這個插入之後的任何異常?如果你使用JPA,如果你真的不需要它,儘量不要使用本地查詢。 –
第一次插入沒有任何問題,但第二次嘗試它有以下異常(因此爲什麼它現在被註釋掉) - org.apache.derby.client.am.SqlException:語句被中止,因爲它會導致'USERBAY'上定義的'SQL140225111513480'標識的唯一或主鍵約束或唯一索引中的重複鍵值。附:它僅用於測試目的,並讓我意識到我絕對不會處理我期望的表格。 – Adrian
因此可能代碼的生成不起作用。嘗試從數據庫中刪除此行並再次執行它。如果一切正常,並且只有在第二次插入時出現問題,則意味着您在生成ID時出現問題。然後試着改變generatedValue策略。 –