jeudi 19 juin 2014

Grails can't databind Set of Strings with underscore in variable name


Vote count:

0




I'm integrating with an existing Java project so I need to use Java classes instead of regular groovy classes in my grails (2.4.0) project.


These Java classes have the variables with a prefix of an underscore before the variable which is fairly common in Java programming.


For example:



String _myString;


I'm attempting to autobind a Set from a form submission and it does not work when the variable is prefixed with an underscore. The getter and setter names remain unchanged. Only the underscore is added to the variable name.


Book.java



@Entity
public class Book {
private int id;
private String title;
private Set<String> skus; //_skus does not work

@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Column(name="TITLE")
public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@ElementCollection
@CollectionTable(
name = "SKU_TABLE",
joinColumns = @JoinColumn(name = "BOOK_ID", nullable = false)
)

@Column(name = "SKU", nullable = false)
public Set<String> getSkus() {
return skus;
}

public void setSkus(Set<String> skus) {
this.skus = skus;
}

}


Sample Script



def paramMap = ['skus[0]' : "123",
'skus[1]' : "456"]

def book = new Book(paramMap)

println book as JSON


Output



{"class":"com.sample.Book","id":0,"skus":["123","456"]}


Output if "skus" has the variable name "_skus"



{"class":"com.sample.Book","id":0,"skus":[]}


asked 28 secs ago

arcdegree

1,055





Aucun commentaire:

Enregistrer un commentaire