活动Jdbc关联不起作用

时间:2016-05-28 01:31:21

标签: java jdbc activejdbc javalite

我有两个名为User和Competencia的类。我想在他们之间建立关系,但它不起作用。参见:

public class User extends Model{

}

@Table(value = "competencias")
@BelongsTo(foreignKeyName = "user_id", parent = User.class)
public class Competencia extends Model{

}
CREATE TABLE competencias(
  id INTEGER NOT NULL DEFAULT NEXTVAL('competencias_id_seq'::regclass),
  mes CHARACTER(2) NOT NULL,
  ano CHARACTER(4) NOT NULL,
  created_at DATE NOT NULL DEFAULT now(),
  user_id INTEGER NOT NULL,
  ativo BOOLEAN,
  CONSTRAINT pk_competencia PRIMARY KEY (id),
  CONSTRAINT fk_competencia_operador FOREIGN KEY (user_id) REFERENCES public.users (id) MATCH SIMPLE ON UPDATE CASCADE ON DELETE NO ACTION,
  CONSTRAINT uk_mes_ano UNIQUE (mes, ano)
)

CREATE TABLE users(
  id INTEGER NOT NULL DEFAULT NEXTVAL('operadores_id_seq'::regclass),
  nome CHARACTER VARYING(25) NOT NULL,
  email CHARACTER VARYING(100) NOT NULL,
  senha CHARACTER VARYING(120) NOT NULL,
  nivel INTEGER NOT NULL DEFAULT 4,
  ativo BOOLEAN,
  created_at DATE DEFAULT now(),
  nome_completo CHARACTER VARYING(60) NOT NULL,
  CONSTRAINT pk_operadores PRIMARY KEY (id),
  CONSTRAINT unique_login UNIQUE (nome)
);

错误讯息:

Exception in thread "AWT-EventQueue-0"  org.javalite.activejdbc.associations.NotAssociatedException: No association from table 'competencias' to table 'users'

完成堆栈跟踪:

Exception in thread "AWT-EventQueue-0" org.javalite.activejdbc.associations.NotAssociatedException: No  association from table 'competencias' to table 'users'
at org.javalite.activejdbc.Model.add(Model.java:2421)

Exception in thread "AWT-EventQueue-0" org.javalite.activejdbc.associations.NotAssociatedException: No association from table 'competencias' to table 'users'
at org.javalite.activejdbc.Model.add(Model.java:2421)
at ui.DialogCompetencia.btSalvarActionPerformed(DialogCompetencia.java:261)
at ui.DialogCompetencia.access$800(DialogCompetencia.java:23)
at ui.DialogCompetencia$10.actionPerformed(DialogCompetencia.java:224)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109)
at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:184)
at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:229)
at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:227)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:227)
at java.awt.Dialog.show(Dialog.java:1084)
at java.awt.Component.show(Component.java:1673)
at java.awt.Component.setVisible(Component.java:1625)
at java.awt.Window.setVisible(Window.java:1014)
at java.awt.Dialog.setVisible(Dialog.java:1005)
at ui.Menu.jMenuItem2ActionPerformed(Menu.java:207)
at ui.Menu.access$100(Menu.java:11)
at ui.Menu$2.actionPerformed(Menu.java:99)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

方法代码:

User user = User.findById(usuario.getId());
Competencia competencia = new Competencia();
competencia.set("id", id);
competencia.set("mes", String.format("%02d", comboMes.getSelectedIndex()));
competencia.set("ano", comboAno.getSelectedItem().toString());
competencia.set("ativo", checkAtivo.isSelected());
**competencia.add(user);**
if(competencia.isValid()){
  if(competencia.saveIt()){
    Functions.informacao("Competencia gravada");
    labelCodigo.setText(String.format("%04d", competencia.getId()));
    preparaForm("salvar");
  }
}else{
  String message = showInfo(competencia);
  JOptionPane.showMessageDialog(this, message, "Corrija os erros abaixo:", JOptionPane.ERROR_MESSAGE);      
}

1 个答案:

答案 0 :(得分:2)

我不确定真正的问题是什么,因为你没有提供日志输出,但你的模型Competencia不应该有注释:

@BelongsTo(foreignKeyName = "user_id", parent = User.class)

因为你遵循惯例。此外,请包括整个堆栈跟踪,而不是第一行,因为它有有用的信息。

更新:

根据您的表结构,您具有一对多关系,其中一个用户具有许多Competencias。

但是,您要为竞争对手添加许多用户:

competencia.add(user);

Competencia没有用户,反之亦然!用户有许多能力。这段代码可以使用:

user.add(competencia);

换句话说,你改变了父/子关系。