为什么建议避免外键上的单向一对多关联?

可能重复: Hibernate单向一对多关联 – 为什么连接表更好? 在Hibernate在线文档中,在第7.2.3节“一对多”中,提到了: 外键上的单向一对多关联是一种不常见的情况,不建议这样做。 您应该使用连接表进行此类关联。 我想知道为什么? 我唯一想到的是,它可以在级联删除期间产生问题。 例如,Person指的是外键上一对多关系的地址,该地址将拒绝在该人之前删除。 任何人都可以解释推荐背后的理性吗? 以下是参考文件内容的链接: 7.2.3。 一到多 我在这里复制粘贴了实际内容: 外键上的单向一对多关联是一种不常见的情况,不建议这样做。 create table Person (personId bigint not null primary key) create table Address (addressId bigint not null primary key, personId bigint not null) 您应该使用连接表进行此类关联。

Java PrintWriter无法正常工作

我只是想把我的二维数组“拼图”写成一个文件。 我有一个双循环,它读取我的数组中的每个’char’值,并据说将它们写入文件。 我似乎无法在我的代码中找到错误。 该文件说它在我运行程序时被修改,但它仍然是空白的。 多谢你们! public void writeToFile(String fileName) { try{ PrintWriter pW = new PrintWriter(new File(fileName)); for(int x = 0; x < 25; x++) { for(int y = 0; y < 25; y++) { pW.write(puzzle[x][y]); } pW.println(); } } catch(IOException e) { System.err.println("error is: "+e.getMessage()); } }

如果app在后台,如何使用fcm从服务器发送数据?

我从服务器向我的应用程序发送fcm通知。 我从服务器发送包含user_id的数据。 如果app在前台,我将在FirebaseMessageService类中获取此userId。 但是当应用程序处于后台时却没有得到它。 由于FirebaseMessagingService类仅在应用程序位于前台时才会执行。 那么当应用程序处于后台时,如何获得此ID? public class MyFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = “MyFirebaseMsgService”; private String mUserId; private Boolean mUpdateNotification; @Override public void onMessageReceived(RemoteMessage remoteMessage) { //Displaying data in log //It is optional Log.d(TAG, “From: ” + remoteMessage.getFrom()); Log.d(TAG, “Notification Message Body: ” + remoteMessage.getNotification().getBody()); String clickAction = remoteMessage.getNotification().getClickAction(); mUserId […]

菱形方算法

我正在尝试用Java编写Diamond-Square算法来生成随机地图,但无法弄清楚实现… 任何有一些Java代码(或其他语言)的人,所以我可以检查如何制作循环将非常感谢! 谢谢!

如何在应用程序外部显示AlertDialog框?

@Override public void run() { //Create thread that can alter the UI AlarmPage.this.runOnUiThread(new Runnable() { public void run() { cal = Calendar.getInstance(); //See if current time matches set alarm time if((cal.get(Calendar.HOUR_OF_DAY) == alarmTime.getCurrentHour()) && (cal.get(Calendar.MINUTE) == alarmTime.getCurrentMinute())){ //If the sound is playing, stop it and rewind if(sound.isPlaying()){ ShowDialog(); alarmTimer.cancel(); alarmTask.cancel(); alarmTask = new PlaySoundTask(); alarmTimer […]

在JTable的单个列上显示MySQL数据库中的图像

我正在尝试显示从blob数据类型的MySQL数据库中检索的图像。 无法弄清楚导致图像列显示数据的问题是什么[B@29b8e4f7而不是图像图标。 DefaultTableModel model = new DefaultTableModel(new Object[]{ “image”, “item_name”, “quantity”,”price”, “category”, “color”, “size”}, 0){ @Override public Class getColumnClass(int column) { switch(column){ case 0: return ImageIcon.class; default: return String.class; } } }; myTable.setModel(model); … ResultSet rs = database.getRS(); int columns = rs.getMetaData().getColumnCount(); while(rs.next()){ Object[] row = new Object[columns]; for(int i = 1; i <= columns; […]

如何根据区域设置格式化YearMonth和MonthDay?

使用特定的Locale在Java 8中格式化LocalDate可以这样实现: DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(myLocale).format(value); DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(myLocale).format(value); DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(myLocale).format(value); DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(myLocale).format(value); 假设value = LocalDate.now()这将导致: // myLocale = Locale.ENGLISH 6/30/16 Jun 30, 2016 June 30, 2016 Thursday, June 30, 2016 // myLocale = Locale.GERMAN 30.06.16 30.06.2016 30. Juni 2016 Donnerstag, 30. Juni 2016 // myLocale = new Locale(“es”, “ES”) 30/06/16 30-jun-2016 30 de junio de 2016 jueves 30 de junio de […]

使用java.util.zip.ZipOutputStream时zip文件中的目录

假设我有一个文件t.txt,一个目录t和另一个文件t / t2.txt。 如果我使用linux zip实用程序“zip -r t.zip t.txt t”,我会得到一个zip文件,其中包含以下条目(unzip -l t.zip): Archive: t.zip Length Date Time Name ——– —- —- —- 9 04-11-09 09:11 t.txt 0 04-11-09 09:12 t/ 15 04-11-09 09:12 t/t2.txt ——– ——- 24 3 files 如果我尝试使用java.util.zip.ZipOutputStream复制该行为并为该目录创建zip条目,则java会抛出exception。 它只能处理文件。 我可以在zip文件中创建/ t2.txt条目并添加使用t2.txt文件内容但我无法创建目录。 这是为什么?

需要Java数组帮助使用scanner类来输出平均值和排序方法

几个小时后,我没有在制作输出平均值的方法上取得进展。 我还需要编写一个排序类。 总的来说,任务需要。 制定方法: ‘main’方法打印数组 对数组进行排序 确定最高值 确定最低值 计算平均值(双倍) import java.util.Scanner; public class Arrayassignment { public static void main(String[] args) { Scanner sin=new Scanner(System.in); System.out.println(“Enter an intiger for array size.”); int number= sin.nextInt(); int array[]=new int[number]; System.out.println(“Array size ” + number + ” initiated.\n”); System.out.println(“Now enter the array intigers”); for (int i = 0; i […]

java.net.SocketException:socket关闭TCP客户端服务器通信

我正在通过TCP创建一个java客户端/服务器应用程序,我们有两个套接字: 一个是交换消息。 二是文件传输。 我在Server中创建了两个ServerSockets以创建Socket One和Two 通过接受ServerSockets。 起初,客户端通过第一个Socket发送一些字节 这样它就可以告诉服务器它需要哪个文件。 然后,服务器通过第二个套接字将文件发送到客户端。 客户端收到文件后,尝试向服务器发送一个Transfer Done Message。 在那里我得到了关闭套接字的exception。 但是我直到现在才关闭套接字。 我只关闭发送文件的缓冲区。 即使我在发送文件后尝试再次打开socketInputStream,错误仍然是相同的。 此外,如果我不关闭发送文件的缓冲区,客户端将无法获取该文件。 服务器exception 返回消息中的错误 – java.net.SocketException:socket已关闭 客户端例外 服务器响应 – java.net.SocketException:软件导致连接中止:套接字写入错误 我究竟做错了什么?