我不知道为什么会收到错误:
org.apache.cayenne.CayenneRuntimeException: [v.4.0.M5 Feb 24 2017 07:47:55] Commit Exception
[...]
Caused by: java.sql.SQLException: Procédure stockée 'auto_pk_for_table' introuvable.
[...]
我使用Cayenne:
<dependency>
<groupId>org.apache.cayenne</groupId>
<artifactId>cayenne-server</artifactId>
<version>4.0.M5</version>
</dependency>
和sms服务器的JDTS:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
联系还可以:
avr. 10, 2017 2:36:30 PM org.apache.cayenne.datasource.DriverDataSource getConnection
INFOS: +++ Connecting: SUCCESS.
我正在尝试创建一个新用户(我从bascis开始!)所以我的代码是: (我削减了一点,太长了:!)
public abstract class _UserInfo extends CayenneDataObject {
public static final String ADDRESS_PROPERTY = "address";
public void setAddress(String address) {
writeProperty(ADDRESS_PROPERTY, address);
}
public String getAddress() {
return (String)readProperty(ADDRESS_PROPERTY);
}
}
public class UserInfo extends _UserInfo implements Serializable {
private static final long serialVersionUID = 1L;
public String address;
public String getAdress() {
return address;
}
public void setAddress(String address) {
super.setAddress(address);
}
//I have the hashcode and equals too
}
然后,我使用vaadin创建我的表单:
public class UserAddView extends CustomComponent implements View {
private static final long serialVersionUID = 1L;
private TextField address;
private Button save;
public static final String USERVIEW = "user";
public boolean checkValidation() {
if (!checkTextFieldValid(address))
return false;
return true;
}
public boolean checkTextFieldValid(TextField element) {
if (element == null || element.isEmpty()) {
Notification.show(
"You should register a " + element.getDescription(),
Type.WARNING_MESSAGE);
return false;
}
return true;
}
public UserAddView() {
VerticalLayout mainLayout = new VerticalLayout();
mainLayout.setSizeFull();
setCompositionRoot(mainLayout);
final VerticalLayout vlayout = new VerticalLayout();
address = new TextField("Address:");
address.setDescription("Address");
vlayout.addComponent(address);
save = new Button("Save");
vlayout.addComponent(save);
mainLayout.addComponent(new HeaderMenu());
mainLayout.addComponent(vlayout);
addListeners();
}
private void addListeners() {
save.addClickListener(new ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void buttonClick(ClickEvent event) {
if (checkValidation() == true) {
ServerRuntime cayenneRuntime = ServerRuntime.builder()
.addConfig("cayenne-myapplication.xml").build();
ObjectContext context = cayenneRuntime.newContext();
UserInfo user = context.newObject(UserInfo.class);
user.setAddress(address.getValue());
user.getObjectContext().commitChanges();
Notification.show(
"Has been saved, We will send you your password by email. Your user login is: "
+ email.getValue(), Type.TRAY_NOTIFICATION);
getUI().getNavigator().navigateTo(HomepageView.MAINVIEW);
}
}
});
}
@Override
public void enter(ViewChangeEvent event) {
// TODO Auto-generated method stub
}
}
编辑,添加信息:在我的用户对象中,我有一个用户ID(主键),在cayenne中我也将它作为主键写入并且在smallint中。此错误似乎是链接... https://cayenne.apache.org/docs/3.1/api/org/apache/cayenne/dba/sybase/SybasePkGenerator.html
答案 0 :(得分:1)
插入新对象时发生错误。对于每个新对象,Cayenne需要生成主键的值。有各种策略可以做到这一点。默认策略取决于您使用的数据库。对于SQLServer(以及Sybase,正如您所发现的:))该策略是使用特殊的存储过程。
要创建此存储过程(以及其他支持DB对象),请转至CayenneModeler,打开项目,然后选择“工具&gt;生成数据库架构”。在“SQL选项”选项卡中,取消选中除“创建主键支持”之外的所有复选框。您将在复选框下方的窗口中看到的SQL是您需要在SQL Server上运行的SQL。可以从Cayenne建模器中进行,也可以复制/粘贴到您喜欢的数据库管理工具。
还有一种不需要存储过程的替代方法 - 使用DB自动增量功能。为此,您需要转到Modeler中的每个DbEntity,并在“Entity”选项卡下选择“Pk Generation Strategy”下拉列表中的“Database-Generated”。这当然意味着您的PK列确实是数据库中的自动增量(这意味着您可能需要相应地调整数据库模式)。