如何在没有多个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
最满意答案
这里有一些代码可以帮助您入门。 我把它全部放在一个类中,但你应该为大型
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 smallJPanels
. This enables you to add the drag and drop logic for the smallJPanels
.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. ...
-
JPanels之间的沟通(communication between JPanels)[2021-09-15]
在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 ...
-
Qt调整码头大小的事件(Qt resize event for docks)[2022-03-21]
如果您不希望子类只是获取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, ...
-
GridBagLayout 2 JPanels - 宽度比其他宽度少一个(GridBagLayout 2 JPanels - One less width than other)[2024-03-29]
看看这个代码示例,看看这是否是您想要的: 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 ... -
5 JPanels的位置(Position of 5 JPanels)[2023-11-23]
您可以使用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 ... -
JPanels AS Docks(JPanels AS Docks)[2023-06-01]
这里有一些代码可以帮助您入门。 我把它全部放在一个类中,但你应该为大型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 ...