小编mom*_*ilo的帖子

ArithmeticException 偶尔被零除

我有一个 JAVA(在 Android 上运行)方法,偶尔会因除以零而捕获 ArithmeticException,尽管变量 inSampleSize 在循环之前设置为 1 并且每次只乘以 2。下面是原样的方法。知道我在这里缺少什么吗?谢谢!

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    try {
        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and …
Run Code Online (Sandbox Code Playgroud)

java android

5
推荐指数
1
解决办法
363
查看次数

DDD在使用Hibernate保存后找出子元素的ID

public class AggregateRoot {
     private Integer id;

     private Set<Child> children;
}

public class Child {
     private Integer id;
     private String name;
}
Run Code Online (Sandbox Code Playgroud)

想象一下,您需要保存Child他的 ID 并将其发送到某个外部系统。在 DDD 中,您将使用与此类似的代码保存孩子:

AggregateRoot aggregateRoot = aggregateRootRepository.getById(id);
Child child = new Child();
child.setName("Sun");
aggregateRoot.addChild(child);
aggregateRootRepository.save(aggregateRoot);
externalService.postSavedChildId(child.getId());
Run Code Online (Sandbox Code Playgroud)

当然child.getId()会返回 null 因为它不在持久化上下文中。知道如何在 DDD 中处理这种情况吗?

java domain-driven-design hibernate

5
推荐指数
1
解决办法
948
查看次数

标签 统计

java ×2

android ×1

domain-driven-design ×1

hibernate ×1