java if long 不起作用

发布于 2024-10-10 16:44:04 字数 1629 浏览 2 评论 0原文

我来到这里的情况真的很奇怪。我有2节课。

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

    @Column(unique=true)
    public String name;
    public Picture picture;
}
  
@Entity
public class PropertyGroupData extends EntityData {
    public Long categoryId;
    public String adminDescription;
    public String title;
    @ManyToMany(fetch=FetchType.EAGER)
    public List properties = new LinkedList();

}

这是我的休眠的实体类。

 
@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 equal 7 但第二个循环不打印任何内容。他们都是龙。我做错了什么?

really strange situation I got here. I have 2 classes.

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

    @Column(unique=true)
    public String name;
    public Picture picture;
}
  
@Entity
public class PropertyGroupData extends EntityData {
    public Long categoryId;
    public String adminDescription;
    public String title;
    @ManyToMany(fetch=FetchType.EAGER)
    public List properties = new LinkedList();

}

this is the entity class for my 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 + "]";
    }
}

here comes the situation. I got some data in my database and it works well. until now.


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);

the first for loop works normally and prints 1-find equal 7 but second loop does not print anything. they both Long. what am I doing wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饭团 2024-10-17 16:44:04

这些表达式的类型为Long,它是一个类,因此通过引用标识进行比较。如果它们很长,那就没问题了。试试这个:

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

The expressions are of type Long, which is a class, and are therefore compared by reference identity. If they were long, it would be fine. Try this instead:

for(CategoryData c:kategoriler)
    if(pgd.categoryId.longValue() == c.id.longValue())
        out.print("2-find equal "+c.id);
孤凫 2024-10-17 16:44:04

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

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

You're comparing 2 objects by reference. That is, you're checking whether they're references to the same object. What you want is to check if they have the same values, e.g.

if(pgd.categoryId.longValue() == c.id.longValue())
娇纵 2024-10-17 16:44:04

由于 Long 会覆盖 .equals,因此您可以使用 .equals() 方法,就像使用字符串一样。

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

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

Since Long overrides the .equals, you can use the .equals() method, just like you do with strings.

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

Using the == just compares the object references.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文