Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果long不起作用,则使用java_Java_Hibernate_If Statement_Long Integer - Fatal编程技术网

如果long不起作用,则使用java

如果long不起作用,则使用java,java,hibernate,if-statement,long-integer,Java,Hibernate,If Statement,Long Integer,我在这里遇到了非常奇怪的情况。我有两门课 @Entity public class CategoryData extends EntityData { public Long parentId; @Column(unique=true) public String name; public Picture picture; } 这是我的hibernate的实体类 @MappedSuperclass public class EntityData im

我在这里遇到了非常奇怪的情况。我有两门课

  
@Entity
public class CategoryData extends EntityData {
    public Long parentId;

    @Column(unique=true)
    public String name;
    public Picture picture;
}
这是我的hibernate的实体类

 
@MappedSuperclass
public class EntityData implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;
    @Temporal(TemporalType.TIMESTAMP)
    public Date created = new Date();
    @Temporal(TemporalType.TIMESTAMP)
    public Date modified = new Date();

    public Long version =  0L;
    // W:waiting,A:active,D:deleted
    public Character status;

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
        //return "EntityData[id=" + id + "]";
    }
}

情况来了。我的数据库中有一些数据,运行良好。直到现在


for(CategoryData c:kategoriler)
    if(pgd.categoryId.toString().equals(c.id.toString()))
        out.print("1-find equal "+c.id);
for(CategoryData c:kategoriler)
    if(pgd.categoryId==c.id)
        out.print("2-find equal "+c.id);

第一个for循环正常工作,打印1-find等于7,但第二个循环不打印任何内容。他们俩都很长。我做错了什么?

表达式的类型为
Long
,这是一个类,因此通过引用标识进行比较。如果它们是
长的
,就可以了。请尝试以下方法:

for(CategoryData c:kategoriler)
    if(pgd.categoryId.longValue() == c.id.longValue())
        out.print("2-find equal "+c.id);

表达式的类型为
Long
,这是一个类,因此通过引用标识进行比较。如果它们是
长的
,就可以了。请尝试以下方法:

for(CategoryData c:kategoriler)
    if(pgd.categoryId.longValue() == c.id.longValue())
        out.print("2-find equal "+c.id);

您正在通过引用比较两个对象。也就是说,您正在检查它们是否是对同一对象的引用。您需要检查它们是否具有相同的值,例如

if(pgd.categoryId.longValue() == c.id.longValue())

您正在通过引用比较两个对象。也就是说,您正在检查它们是否是对同一对象的引用。您需要检查它们是否具有相同的值,例如

if(pgd.categoryId.longValue() == c.id.longValue())

因为Long重写了.equals,所以可以使用.equals()方法,就像处理字符串一样

for(CategoryData c:kategoriler)
{
  if (pgd.categoryId.equals(c.id))
  {
      out.print("2-find equal "+c.id);
  }
}

使用==只是比较对象引用。

因为Long重写了.equals,所以可以使用.equals()方法,就像处理字符串一样

for(CategoryData c:kategoriler)
{
  if (pgd.categoryId.equals(c.id))
  {
      out.print("2-find equal "+c.id);
  }
}

使用==只会比较对象引用。

这些基本值与对象值所引起的问题比它应该引起的问题多得多。这些基本值与对象值所引起的问题比它应该引起的问题多得多。