java if long 不起作用
我来到这里的情况真的很奇怪。我有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些表达式的类型为
Long
,它是一个类,因此通过引用标识进行比较。如果它们很长,那就没问题了。试试这个:The expressions are of type
Long
, which is a class, and are therefore compared by reference identity. If they werelong
, it would be fine. Try this instead:您正在通过引用比较 2 个对象。也就是说,您正在检查它们是否是对同一对象的引用。你想要的是检查它们是否具有相同的值,例如
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.
由于 Long 会覆盖 .equals,因此您可以使用 .equals() 方法,就像使用字符串一样。
使用 == 只是比较对象引用。
Since Long overrides the .equals, you can use the .equals() method, just like you do with strings.
Using the == just compares the object references.