Room DB-在事务内部插入M2M关系

时间:2018-07-07 12:04:48

标签: android sql transactions android-room

我正在将新书插入我的书表中,并尝试将其分配给多对多关系表后。伊莫这应该在交易中运行。 (因为如果m2m插入失败,则会丢失有关房地产的信息)。现在,我的代码如下所示,由于由于静态上下文错误而无法访问public class Main { private JFrame mainPane; private RenderZone renderZone = new RenderZone(); private JScrollPane renderScrollPane = new JScrollPane(renderZone, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Main window = new Main(); window.mainPane.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public Main() { initialize(); } private void initialize() { mainPane = new JFrame(); mainPane.setPreferredSize(new Dimension(1024, 768)); mainPane.setSize(new Dimension(1024, 768)); mainPane.setLocation(new Point(200, 200)); mainPane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SpringLayout springLayout = new SpringLayout(); springLayout.putConstraint(SpringLayout.NORTH, renderScrollPane, 0, SpringLayout.NORTH, mainPane.getContentPane()); springLayout.putConstraint(SpringLayout.WEST, renderScrollPane, 0, SpringLayout.WEST, mainPane.getContentPane()); springLayout.putConstraint(SpringLayout.SOUTH, renderScrollPane, 0, SpringLayout.SOUTH, mainPane.getContentPane()); springLayout.putConstraint(SpringLayout.EAST, renderScrollPane, 0, SpringLayout.EAST, mainPane.getContentPane()); mainPane.getContentPane().setLayout(springLayout); mainPane.getContentPane().add(renderScrollPane); } public class RenderZone extends JPanel { private static final long serialVersionUID = -8240875812801921009L; private BufferedImage image = null; private BufferedImage sub = null; public RenderZone() { this.setBackground(Color.WHITE); this.setPreferredSize(new Dimension(1124, 768));// added 100 for test purpose } @Override protected void paintComponent(Graphics g) { super.paintComponent(g);// pour repeindre la panel à chaque fois Graphics2D g2 = (Graphics2D)this.getGraphics(); if(image==null) { try { image = ImageIO.read(RenderZone.class.getResourceAsStream("TestImage0.png")); sub = image.getSubimage(0, 0, 1024, 768); } catch (IOException e) {} } if( sub!=null)g2.drawImage(sub, 0, 0, null); g2.dispose(); } } } 查询,因此代码失败。

有一种简单的方法可以解决此问题吗?

BookUserXRefDao.insert(bookUser);

1 个答案:

答案 0 :(得分:0)

仅仅意识到我可以从我的交易中访问Singleton数据库实例。

因此,我可以使用

AppDb.getAppDb().BookUserXRefDao().insert(bookUser);

那解决了这个问题。