首页 \ 问答 \ 如何在没有多个cfg.xml文件的情况下通过hibernate处理多个数据库(How to handle multiple database by hibernate without multiple cfg.xml files)

如何在没有多个cfg.xml文件的情况下通过hibernate处理多个数据库(How to handle multiple database by hibernate without multiple cfg.xml files)

我们有一个基于Java-Hibernate的Web应用程序,我们在其中处理PostgreSQL数据库。(它是一个包含多个模式的单个数据库。)

目前我们正面临可伸缩性问题,因此我们决定在多个数据库之间拆分模式。

示例

  • 以前我们有一个名为“mydb”的数据库。
  • 现在我们拆分数据库如“mydb_1”,“mydb_2”,“mydb_3”等等......(因为负载会增加,我们将创建新的数据库。)

问题

  • 如何从一个* .cfg.xml管理多个数据库? (我们不想每次都为每个数据库引入多个* .cfg.xml。)
  • 这可以用更新的hibernate版本来实现吗? (我们准备升级我们的Hibernate版本)
  • 我们需要创建会话工厂池吗? (我认为这是一个坏主意和思想......))

注意:如果有人可以为此提供教程/文档链接,那将是很棒的。

类似问题: Hibernate使用多个数据库

(我期待类似的答案,但没有多个cfg.xml :()


We have a Java-Hibernate based web application in which we deal with PostgreSQL database.(It is a single database having multiple schema(s) in it.)

Currently we are facing scalability issues so we have decided to split our schema(s) among multiple database.

Example:

  • Previously we have database called "mydb".
  • And now we split database like "mydb_1", "mydb_2", "mydb_3", so on... (As load will increase, we will create new database.)

Problem:

  • How to manage multiple database from one *.cfg.xml? (We do not want to introduce multiple *.cfg.xml (for every database) every time.)
  • Can this be achieved with newer hibernate version? (We are ready to upgrade our Hibernate version)
  • Do we need to create pool of session factory? (I think its a bad idea and thought... :) )

Note: It would be great, if anyone can provide tutorial/document link for this.

Similar Question: Hibernate using multiple databases

(I am expecting similar kind of answer but without multiple cfg.xml :( )


原文:https://stackoverflow.com/questions/42951474
更新时间:2022-11-16 10:11

最满意答案

这里有一些代码可以帮助您入门。 我把它全部放在一个类中,但你应该为大型JPanel和小型JPanels制作单独的类。 这使您可以为小型JPanels添加拖放逻辑。

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

public class TVGrid implements Runnable {

    protected static final Insets noInsets    = new Insets(0, 0, 0, 0);

    protected Dimension screenSize;

    protected JFrame frame;

    protected JPanel largePanel;
    protected JPanel mainPanel;

    protected JPanel[] smallPanels;

    protected Point[] smallPanelOrigins;

    public TVGrid() {
        this.smallPanels = new JPanel[7];
        setSmallPanelOrigins();
        this.screenSize = Toolkit.getDefaultToolkit().getScreenSize();      
    }

    protected void setSmallPanelOrigins() {
        this.smallPanelOrigins = new Point[7];
        this.smallPanelOrigins[0] = new Point(3, 0);
        this.smallPanelOrigins[1] = new Point(3, 1);
        this.smallPanelOrigins[2] = new Point(3, 2);
        this.smallPanelOrigins[3] = new Point(0, 3);
        this.smallPanelOrigins[4] = new Point(1, 3);
        this.smallPanelOrigins[5] = new Point(2, 3);
        this.smallPanelOrigins[6] = new Point(3, 3);
    }

    @Override
    public void run() {
        Border border = BorderFactory.createLineBorder(Color.BLACK, 3);

        frame = new JFrame();
        frame.setTitle("TV Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());

        largePanel = new JPanel();
        largePanel.setPreferredSize(getLargePanelPreferredSize());
        largePanel.setBackground(Color.GREEN);
        largePanel.setBorder(border);

        addComponent(mainPanel, largePanel, 0, 0, 3, 3, noInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.BOTH);

        for (int i = 0; i < 7; i++) {
            smallPanels[i] = new JPanel();
            smallPanels[i].setPreferredSize(getSmallPanelPreferredSize());
            smallPanels[i].setBackground(Color.YELLOW);
            smallPanels[i].setBorder(border);

            int x = smallPanelOrigins[i].x;
            int y = smallPanelOrigins[i].y;

            addComponent(mainPanel, smallPanels[i], x, y, 1, 1, noInsets,
                    GridBagConstraints.LINE_START, GridBagConstraints.BOTH);
        }

        frame.add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }

    protected Dimension getLargePanelPreferredSize() {
        Dimension d = new Dimension();
        d.width = ((screenSize.width - 30) * 3) / 4;
        d.height = ((screenSize.height - 90) * 3) / 4;
        return d;
    }

    protected Dimension getSmallPanelPreferredSize() {
        Dimension d = new Dimension();
        d.width = (screenSize.width - 30) / 4;
        d.height = (screenSize.height - 90) / 4;
        return d;
    }

    protected void addComponent(Container container, Component component,
            int gridx, int gridy, int gridwidth, int gridheight, 
            Insets insets, int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
        container.add(component, gbc);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TVGrid());
    }


}

Here's some code to get you started. I put it all in one class, but you should make separate classes for the large JPanel and the small JPanels. This enables you to add the drag and drop logic for the small JPanels.

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

public class TVGrid implements Runnable {

    protected static final Insets noInsets    = new Insets(0, 0, 0, 0);

    protected Dimension screenSize;

    protected JFrame frame;

    protected JPanel largePanel;
    protected JPanel mainPanel;

    protected JPanel[] smallPanels;

    protected Point[] smallPanelOrigins;

    public TVGrid() {
        this.smallPanels = new JPanel[7];
        setSmallPanelOrigins();
        this.screenSize = Toolkit.getDefaultToolkit().getScreenSize();      
    }

    protected void setSmallPanelOrigins() {
        this.smallPanelOrigins = new Point[7];
        this.smallPanelOrigins[0] = new Point(3, 0);
        this.smallPanelOrigins[1] = new Point(3, 1);
        this.smallPanelOrigins[2] = new Point(3, 2);
        this.smallPanelOrigins[3] = new Point(0, 3);
        this.smallPanelOrigins[4] = new Point(1, 3);
        this.smallPanelOrigins[5] = new Point(2, 3);
        this.smallPanelOrigins[6] = new Point(3, 3);
    }

    @Override
    public void run() {
        Border border = BorderFactory.createLineBorder(Color.BLACK, 3);

        frame = new JFrame();
        frame.setTitle("TV Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());

        largePanel = new JPanel();
        largePanel.setPreferredSize(getLargePanelPreferredSize());
        largePanel.setBackground(Color.GREEN);
        largePanel.setBorder(border);

        addComponent(mainPanel, largePanel, 0, 0, 3, 3, noInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.BOTH);

        for (int i = 0; i < 7; i++) {
            smallPanels[i] = new JPanel();
            smallPanels[i].setPreferredSize(getSmallPanelPreferredSize());
            smallPanels[i].setBackground(Color.YELLOW);
            smallPanels[i].setBorder(border);

            int x = smallPanelOrigins[i].x;
            int y = smallPanelOrigins[i].y;

            addComponent(mainPanel, smallPanels[i], x, y, 1, 1, noInsets,
                    GridBagConstraints.LINE_START, GridBagConstraints.BOTH);
        }

        frame.add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }

    protected Dimension getLargePanelPreferredSize() {
        Dimension d = new Dimension();
        d.width = ((screenSize.width - 30) * 3) / 4;
        d.height = ((screenSize.height - 90) * 3) / 4;
        return d;
    }

    protected Dimension getSmallPanelPreferredSize() {
        Dimension d = new Dimension();
        d.width = (screenSize.width - 30) / 4;
        d.height = (screenSize.height - 90) / 4;
        return d;
    }

    protected void addComponent(Container container, Component component,
            int gridx, int gridy, int gridwidth, int gridheight, 
            Insets insets, int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
        container.add(component, gbc);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TVGrid());
    }


}

相关问答

更多
  • 我想知道在主JPanel上添加4个JPanel是否正确? 当然。 您看到的大多数Java GUI都不仅仅是将面板放在其他面板中。 有时,使用不同的面板对常用控件或输出组件进行分组,并可能为它们提供标题边框。 更常见的是,使用不同的面板以便在GUI的不同部分中使用不同的布局管理器。 这是一个众所周知的示例 ,它将布局的细节放在面板的标题边框中。 I want to know whether it's correct or not to add 4 JPanels on a main JPanel? Sure. ...
  • 在Panel类型的myJPanel1类中添加一个新字段,以便您可以参考Jpanel2,这样您就可以进一步使用它而无需更改Panel 2中的任何内容。请按照下列步骤操作: 添加名为Panel panel2的新字段,并在构造函数中添加Panel参数,如myJPanel1(JPanel panel) {//set local panel2 instance variable now} 在myJPanel类中,更改以下内容: myJPanel1 p1 = new myJPanel1(); add(p1,"North ...
  • 使用组合框在Jpanel之间切换。 使用卡布局在面板之间切换。 教程示例向您展示了如何执行此操作。 另一种方法是使用JTabbed窗格 。 本教程再次展示了如何将面板添加到其他选项卡。 using combobox to switch between Jpanels. Use a Card Layout to switch between panels. The tutorial example shows you exactly how to do this. The other approach is ...
  • 如果您要将JLabel添加到JPanel ,那么您需要将其包装在JPanel内部。 并且,在zPanel所有子zPanel时添加zPanel 。 JPanel zPanel = new JPanel(new MigLayout("wrap 1"); for(int i = 0; i < zones.size(); i++ ) { JLabel zLabel = new JLabel(zones.get(i)); zPanel.add(zLabel); } grid.add(zPanel,"w ...
  • 如果您不希望子类只是获取resize事件控件,则可以installEventFilter 小例子看起来像( MainWindow.h ):( MainWindow在这里持有DockWidget) protected: bool eventFilter(QObject *obj, QEvent *event); 和( MainWindow.cc ): MainWindow::MainWindow(QWidget* parent) : QWidget(parent) { ui_.setupUi ...
  • 最后,我提出了一个解决方案: 创建一个方法来调整Table类中的大小:此方法检索每个表列的宽度,并分别设置每个面板的宽度。 向Table类添加一个组件侦听器,并在componentResized上调用该方法。 而已。 Finally, I came up with a solution: Create a method to adjust sizes in the Table class: This method retrieves the width of each table column, and s ...
  • 我使用BoxLayout得到它。 import java.awt.Color; public class ProportionalPanels extends JFrame { private JPanel contentPane; /** * Create the frame. */ public ProportionalPanels() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, ...
  • 看看这个代码示例,看看这是否是您想要的: import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; public class GridBagExample { private JPanel leftPanel; private JPanel rightPanel; private GridBagConstraints gbc; private Ran ...
  • 您可以使用GridLayout 。 通过GridLayout(int rows, int cols, int hgap, int vgap)构造函数,您可以定义垂直和水平间隙。 JPanel mainPanel = new JPanel(new GridLayout(5, 1, 0, 5)); 并根据您的更新; 您必须将contentPane设置为您的框架。 frame.setContentPane(contenPane); You can use GridLayout. Wtih GridLayout ...
  • 这里有一些代码可以帮助您入门。 我把它全部放在一个类中,但你应该为大型JPanel和小型JPanels制作单独的类。 这使您可以为小型JPanels添加拖放逻辑。 import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; imp ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)