目前,我的项目使用@Enumerated(EnumType.ORDINAL),所以当我按此列排序时,它是根据枚举中的顺序排序的,这工作正常。但我需要向 enum 添加一些额外的值,这些值需要插入到枚举值列表的不同位置,不能只添加到底部以保持正确的排序顺序。
如果我这样做,我的数据库就会乱七八糟。我将不得不编写一些脚本来将所有这些序数值转换为正确的新序数。有可能以后必须添加更多状态。由于我必须修复数据库中的所有数据,因此我希望只需执行一次,因为这将是一项艰巨的任务。
所以我正在考虑切换到 EnumType.STRING,这样就不必再次重新映射数据库中的序数值。但是如果我这样做,那么我该如何正确排序呢?枚举字符串的字母顺序不是我需要的顺序。
使用下面的类,当我按属性“status”排序时,结果按以下顺序出现:
hql = "from Project order by status"
Development
Planning
Production
我希望它们按此顺序出现,而不使用 EnumType.ORDINAL:
Planning
Development
Production
如果不为 enum 创建一个表,或者在 Project 类中添加一个额外的列,这是否可能?我试过这个,但它抛出一个异常:
hql = "from Project order by status.sort"
枚举:
public enum Status {
PLANNING("Planning", 0),
DEVELOPMENT("Development", 1),
PRODUCTION("Production", 2);
private final String name;
private final int sort;
private Status(String name, int sort) {
this.name = name;
this.sort = sort;
}
@Override
public String toString() {
return name;
}
}
实体:
@Entity
public class Project {
private Long id;
private Status status;
@Id
@GeneratedValue
public Long getId() {
return this.id;
}
private void setId(Long id) {
this.id = id;
}
@Enumerated(EnumType.STRING)
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
最佳答案
So I'm thinking of switching to EnumType.STRING to not have to remap ordinal values in the database again. But if I do this, then how do I sort properly? The alphabetical order of the enum strings is not the order I need.
我个人完全避免使用邪恶的 EnumType.ORDINAL,因为仅仅改变常量的顺序就会破坏持久性逻辑。邪恶的。也就是说,EnumType.STRING 确实并不总是合适的。
在您的情况下,这是我会做的(使用标准 JPA):我会在实体级别保留 int 并在 getter/setter 中执行枚举转换。像这样的东西。首先是枚举:
public enum Status {
PLANNING("Planning", 100),
DEVELOPMENT("Development", 200),
PRODUCTION("Production", 300);
private final String label;
private final int code;
private Status(String label, int code) {
this.label = label;
this.code = code;
}
public int getCode() { return this.code; }
private static final Map<Integer,Status> map;
static {
map = new HashMap<Integer,Status>();
for (Status v : Status.values()) {
map.put(v.code, v);
}
}
public static Status parse(int i) {
return map.get(i);
}
}
所以基本上,这个想法是能够通过其code 获得Status。我们在常量之间保留了一些空间,以便可以添加值(这不是很漂亮,但是,它会工作并且在一段时间内应该是安全的)。
然后在实体中:
@Entity
public class Project {
private Long id;
private int statusCode;
@Id @GeneratedValue
public Long getId() {
return this.id;
}
private void setId(Long id) {
this.id = id;
}
@Transient
public Status getStatus () {
return Status.parse(this.statusCode);
}
public void setStatus(Status status) {
this.statusCode = status.getCode();
}
protected int getStatusCode() {
return statusCode;
}
protected void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
}
int 表示的 getter/setter 受到保护。公共(public) getter/setter 处理转换。
另一种解决方案是使用自定义类型(以可移植性为代价)。
关于java - 控制 Hibernate EnumType.STRING 属性的排序顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3605655/
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少
我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems
我有这个html标记:我想得到这个:我如何使用Nokogiri做到这一点? 最佳答案 require'nokogiri'doc=Nokogiri::HTML('')您可以通过xpath删除所有属性:doc.xpath('//@*').remove或者,如果您需要做一些更复杂的事情,有时使用以下方法遍历所有元素会更容易:doc.traversedo|node|node.keys.eachdo|attribute|node.deleteattributeendend 关于ruby-Nokog
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs