I have the following code:
PersonDao.java
#Repository
#Transactional
public class PersonDao implements PersonDaoIface {
Object property;
String order;
#Autowired
private SessionFactory sessionFactory;
public PersonDao() {
}
public PersonDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#SuppressWarnings("unchecked")
#Override
public List<Person> getAll(long first, long count) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class);
this.setPaging(criteria, first, count);
addSort(criteria);
return criteria.list();
}
#Override
public long getAllCount() {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class)
.setProjection(Projections.rowCount());
Long i = (Long) criteria.uniqueResult();
return i;
}
#Override
public List<Person> getByFilter(Person person, int first, int count) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class);
criteria.add(Restrictions.eq("firstName", person.getFirstName()));
criteria.add(Restrictions.eq("lastName", person.getLastName()));
this.setPaging(criteria, first, count);
return criteria.list();
}
#Override
public long getByFilterCount(Person person) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class);
criteria.add(Restrictions.eq("firstName", person.getFirstName()));
criteria.add(Restrictions.eq("lastName", person.getLastName()));
criteria.setProjection(Projections.rowCount());
Long result = (Long) criteria.uniqueResult();
return result;
}
private void setPaging(Criteria criteria, long first, long count) {
criteria.setFirstResult((int) first);
criteria.setMaxResults((int) count);
}
private void addSort(Criteria criteria) {
if (property != null) {
if (order.equalsIgnoreCase(SortOrder.ASCENDING.toString())) {
criteria.addOrder(Order.asc((String)property));
} else {
criteria.addOrder(Order.desc((String)property));
}
}
}
#Override
public void setSort(Object property, String order) {
this.property = property;
this.order = order;
}
}
SortableDataProvider
public class PersonSortableDataProvider extends SortableDataProvider {
private transient PersonDaoIface personDao;
public PersonSortableDataProvider(PersonDaoIface personDao) {
this.personDao = personDao;
}
public PersonSortableDataProvider() {
}
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public Iterator<Person> iterator(long first, long count) {
System.out.println(getSort());
return personDao.getAll(first, count).iterator();
}
#Override
public long size() {
long result = personDao.getAllCount();
return result;
}
#Override
public IModel<Person> model(final Object object) {
return new AbstractReadOnlyModel<Person>() {
#Override
public Person getObject() {
return (Person) object;
}
};
}
}
A panel with a data table using the sortable data provider
public DataDisplayPanel(String id) {
super(id);
List<IColumn> columns = new ArrayList<IColumn>();
columns.add(new PropertyColumn(new Model<String>("First Name"), "firstName"));
columns.add(new PropertyColumn(new Model<String>("Last Name"), "lastName"));
AjaxFallbackDefaultDataTable table = new AjaxFallbackDefaultDataTable("personData", columns,
personSortableDataProvider, 8);
table.addTopToolbar(new HeadersToolbar(table, personSortableDataProvider));
add(table);
}
I have paging done no problem but I am having trouble understanding how to get sorting working with hibernate, I can see how you could do the sorting from the java side of things but given that I could potentially get large data sets back I don't like this idea.
Given my code above does anyone have a way of getting the data table, on click of either first name or last name to then make the same query found in the iterator with the additional order by clause.
You are almost there. You just need an:
addOrder(Order.asc(columnName))
The doc is here.
To anyone that encounters this situation I have the following setup:
hibernate 4, spring 4 and wicket 6
I inject using Spring and it seems wicket and spring get confused if you inject within the SortableDataProvider.
I don't know what exactly happens; when i step over the project I will have a better idea but it appears setSort is not getting set correctly, when i move the Dao class out of sortable data provider and into the page and inject it there, then pass the dao instance into sortable data provider the sorting works correctly.
Related
I want to dynamic search with Criteria API in Java.
In the code I wrote, we need to write each entity in the url bar in JSON. I don't want to write "plaka".
The URL : <localhost:8080/api/city/query?city=Ankara&plaka=> I want to only "city" or "plaka"
Here we need to write each entity, even if we are going to search with only 1 entity. Type Entity and it should be empty.
My code is as below. Suppose there is more than one entity, what I want to do is to search using a single entity it wants to search. As you can see in the photo, I don't want to write an entity that I don't need. can you help me what should I do?
My code in Repository
public interface CityRepository extends JpaRepository<City, Integer> , JpaSpecificationExecutor<City> {
}
My code in Service
#Service
public class CityServiceImp implements CityService{
private static final String CITY = "city";
private static final String PLAKA = "plaka";
#Override
public List<City> findCityByNameAndPlaka(String cityName, int plaka) {
GenericSpecification genericSpecification = new GenericSpecification<City>();
if (!cityName.equals("_"))
genericSpecification.add(new SearchCriteria(CITY,cityName, SearchOperation.EQUAL));
if (plaka != -1)
genericSpecification.add(new SearchCriteria(PLAKA,plaka, SearchOperation.EQUAL));
return cityDao.findAll(genericSpecification);
}
#Autowired
CityRepository cityDao;
My code in Controller
#RestController
#RequestMapping("api/city")
public class CityController {
#Autowired
private final CityService cityService;
public CityController(CityService cityService) {
this.cityService = cityService;
#GetMapping("/query")
public List<City> query(#RequestParam String city, #RequestParam String plaka){
String c = city;
int p;
if (city.length() == 0)
c = "_";
if (plaka.length() == 0) {
p = -1;
}
else
p = Integer.parseInt(plaka);
return cityService.findCityByNameAndPlaka(c,p);
}
My code in SearchCriteria
public class SearchCriteria {
private String key;
private Object value;
private SearchOperation operation;
public SearchCriteria(String key, Object value, SearchOperation operation) {
this.key = key;
this.value = value;
this.operation = operation;
}
public String getKey() {
return key;
}
public Object getValue() {
return value;
}
public SearchOperation getOperation() {
return operation;
}
My code in GenericSpecification
public class GenericSpecification<T> implements Specification<T> {
private List<SearchCriteria> list;
public GenericSpecification() {
this.list = new ArrayList<>();
}
public void add(SearchCriteria criteria){
list.add(criteria);
}
#Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
List<Predicate> predicates = new ArrayList<>();
for (SearchCriteria criteria : list) {
if (criteria.getOperation().equals(SearchOperation.GREATER_THAN)) {
predicates.add(builder.greaterThan(
root.get(criteria.getKey()), criteria.getValue().toString()));
} else if (criteria.getOperation().equals(SearchOperation.LESS_THAN)) {
predicates.add(builder.lessThan(
root.get(criteria.getKey()), criteria.getValue().toString()));
} else if (criteria.getOperation().equals(SearchOperation.GREATER_THAN_EQUAL)) {
predicates.add(builder.greaterThanOrEqualTo(
root.get(criteria.getKey()), criteria.getValue().toString()));
} else if (criteria.getOperation().equals(SearchOperation.LESS_THAN_EQUAL)) {
predicates.add(builder.lessThanOrEqualTo(
root.get(criteria.getKey()), criteria.getValue().toString()));
} else if (criteria.getOperation().equals(SearchOperation.NOT_EQUAL)) {
predicates.add(builder.notEqual(
root.get(criteria.getKey()), criteria.getValue()));
} else if (criteria.getOperation().equals(SearchOperation.EQUAL)) {
predicates.add(builder.equal(
root.get(criteria.getKey()), criteria.getValue()));
} else if (criteria.getOperation().equals(SearchOperation.MATCH)) {
predicates.add(builder.like(
builder.lower(root.get(criteria.getKey())),
"%" + criteria.getValue().toString().toLowerCase() + "%"));
} else if (criteria.getOperation().equals(SearchOperation.MATCH_END)) {
predicates.add(builder.like(
builder.lower(root.get(criteria.getKey())),
criteria.getValue().toString().toLowerCase() + "%"));
}
}
return builder.and(predicates.toArray(new Predicate[0]));
}
My code in SearchOperation
public enum SearchOperation {
GREATER_THAN,
LESS_THAN,
GREATER_THAN_EQUAL,
LESS_THAN_EQUAL,
NOT_EQUAL,
EQUAL,
MATCH,
MATCH_END,
}
The good thing about the Criteria API is that you can use the CriteriaBuilder to build complex SQL statements based on the fields that you have. You can combine multiple criteria fields using and and or statements with ease.
How I approached something similar int he past is using a GenericDao class that takes a Filter that has builders for the most common operations (equals, qualsIgnoreCase, lessThan, greaterThan and so on). I actually have something similar in an open-source project I started: https://gitlab.com/pazvanti/logaritmical/-/blob/master/app/data/dao/GenericDao.java
https://gitlab.com/pazvanti/logaritmical/-/blob/master/app/data/filter/JPAFilter.java
Next, the implicit DAO class extends this GenericDao and when I want to do an operation (ex: find a user with the provided username) and there I create a Filter.
Now, the magic is in the filter. This is the one that creates the Predicate.
In your request, you will receive something like this: field1=something&field2=somethingElse and so on. The value can be preceded by the '<' or '>' if you want smaller or greater and you initialize your filter with the values. If you can retrieve the parameters as a Map<String, String>, even better.
Now, for each field in the request, you create a predicate using the helper methods from the JPAFilter class and return he resulted Predicate. In the example below I assume that you don't have it as a Map, but as individual fields (it is easy to adapt the code for a Map):
public class SearchFilter extends JPAFilter {
private Optional<String> field1 = Optional.empty();
private Optional<String> field2 = Optional.empty();
#Override
public Predicate getPredicate(CriteriaBuilder criteriaBuilder, Root root) {
Predicate predicateField1 = field1.map(f -> equals(criteriaBuilder, root, "field1", f)).orElse(null);
Predicate predicateField2 = field2.map(f -> equals(criteriaBuilder, root, "field2", f)).orElse(null);
return andPredicateBuilder(criteriaBuilder, predicateField1, predicateField2);
}
}
Now, I have the fields as Optional since in this case I assumed that you have them as Optional in your request mapping (Spring has this) and I know it is a bit controversial to have Optional as input params, but in this case I think it is acceptable (more on this here: https://petrepopescu.tech/2021/10/an-argument-for-using-optional-as-input-parameters/)
The way the andPredicateBuilder() is made is that it works properly even if one of the supplied predicates is null. Also, I made s simple mapping function, adjust to include for < and >.
Now, in your DAO class, just supply the appropriate filter:
public class SearchDao extends GenericDAO {
public List<MyEntity> search(Filter filter) {
return get(filter);
}
}
Some adjustments need to be made (this is just starter code), like an easier way to initialize the filter (and doing this inside the DAO) and making sure that that the filter can only by applied for the specified entity (probably using generics, JPAFIlter<T> and having SearchFilter extends JPAFilter<MyEntity>). Also, some error handling can be added.
One disadvantage is that the fields have to match the variable names in your entity class.
I'm developing this application to fetch data from a single table from an existing Oracle database.
Here we've got the entity:
public class OrdemDeServicoCount {
private Long ordensInternas;
private Long ordensAtrasadas;
// assume getters and setters
}
The mapper:
public class OrdemMapper implements RowMapper<OrdemDeServicoCount> {
#Override
public OrdemDeServicoCount mapRow(ResultSet rs, int rowNum) throws SQLException {
OrdemDeServicoCount ordens = new OrdemDeServicoCount();
ordens.setOrdensInternas(rs.getLong("ordensInternas"));
// ordens.setOrdensAtrasadas(rs.getLong("ordensAtrasadas"));
return ordens;
}
}
And finally, the DAO:
public class OrdemDAO {
private JdbcTemplate jdbcTemplate;
public OrdemDAO(JdbcTemplate jdbcTemplate) {
super();
this.jdbcTemplate = jdbcTemplate;
}
public List<OrdemDeServicoCount> countOrdensInternasSemEncerrar() {
String sql = "SELECT COUNT(a.nr_sequencia) AS ordensInternas FROM MAN_ORDEM_SERVICO a "
+ "WHERE a.IE_STATUS_ORDEM IN (1,2) AND a.NR_GRUPO_PLANEJ IN (21)";
List<OrdemDeServicoCount> ordens = jdbcTemplate.query(sql, new OrdemMapper());
return ordens;
}
By the way, you all must know that if I declare uncomment the line ordens.setOrdensInternas(rs.getLong("ordensInternas")); in the mapper, I would get an error, because in my DAO, I'm not using that field.
But what if I need to create another method that uses just the ordensInternas field? Then again, I'd get an error...
So, my doubt here is: if I need to use the ordensAtrasadas field from the entity, will I have to create another class just to implement another mapper? Or is there a way that I can do any conditional in my current OrdemMapper class?
Just put your assignments in individual try-catch statements.
public class OrdemMapper implements RowMapper<OrdemDeServicoCount> {
#Override
public OrdemDeServicoCount mapRow(ResultSet rs, int rowNum) throws SQLException {
OrdemDeServicoCount ordens = new OrdemDeServicoCount();
try {
ordens.setOrdensInternas(rs.getLong("ordensInternas"));
} catch (SQLException ex) {
// This will happen if the columnIndex is invalid among other things
}
try {
ordens.setOrdensAtrasadas(rs.getLong("ordensAtrasadas"));
} catch (SQLException ex) {
// This will happen if the columnIndex is invalid among other things
}
return ordens;
}
}
I have AOP aspect for counting times some service was called:
#Aspect
#Component
public class CounterAspect {
private Map<Integer, Integer> gettingEventStatistics = new HashMap<>();
#Pointcut("execution(Event EventService+.getById(Integer))")
private void gettingEvent() {}
#AfterReturning(pointcut = "gettingEvent()", returning = "retVal")
public void countGettingEvent(JoinPoint joinPoint, Object retVal) {
Integer id = (Integer) joinPoint.getArgs()[0];
if (id != null && retVal != null) {
Integer currentCounterValue = gettingEventStatistics.get(id);
gettingEventStatistics.put(id, currentCounterValue == null ? 1 : currentCounterValue + 1);
}
}
}
How could I store such an information in DB using Hibernate?
I've made the following solution - created entity for stats
#Entity
public class GettingEventsStats {
#Id
private Integer eventId;
private Integer gettingCounter;
//getters, setters, etc.
injected DAO delegate in my aspect with such a functionality:
#Repository
public class HibernateStatsDao implements StatsDao {
#Autowired
private SessionFactory sessionFactory;
#Override
public GettingEventsStats getGettingEventStats(Integer eventId) {
return sessionFactory.getCurrentSession().get(GettingEventsStats.class, eventId);
}
#Override
public void createGettingEventCounter(Integer eventId) {
GettingEventsStats gettingEventsStats = new GettingEventsStats();
gettingEventsStats.setEventId(eventId);
gettingEventsStats.setGettingCounter(1);
sessionFactory.getCurrentSession().save(gettingEventsStats);
}
#Override
public void updateGettingEventCounter(Integer eventId) {
GettingEventsStats gettingEventStats = getGettingEventStats(eventId);
gettingEventStats.setGettingCounter(gettingEventStats.getGettingCounter() + 1);
sessionFactory.getCurrentSession().update(gettingEventStats);
}
}
and changed aspect logic to:
#Autowired
private StatsDao statsDao;
#Pointcut("execution(Event EventService+.getById(Integer))")
private void gettingEvent() {}
#AfterReturning(pointcut = "gettingEvent()", returning = "retVal")
public void countGettingEvent(JoinPoint joinPoint, Object retVal) {
Integer eventId = (Integer) joinPoint.getArgs()[0];
if (eventId != null && retVal != null) {
GettingEventsStats gettingEventStats = statsDao.getGettingEventStats(eventId);
if (gettingEventStats == null) {
statsDao.createGettingEventCounter(eventId);
} else {
statsDao.updateGettingEventCounter(eventId);
}
}
}
You are going to save a lot of entries with this method. Better use Dropwizard Metrics which uses Reservoirs for data sampling and custom reporters.
Typically, the metrics are better off exposed through JMX to an APM tool. Or you should use Graphite or Graphana for this purpose.
The simplest way would be parse it to JSON and store as a string. Remember then to increase column max memory size.
I've created a class User that extends Document. User just has some simple constructors and getters/setters around some strings and ints. However, when I try to insert the User class into Mongo I get the following error:
Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.foo.User.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
at org.bson.BsonDocumentWrapper.asBsonDocument(BsonDocumentWrapper.java:62)
at com.mongodb.MongoCollectionImpl.documentToBsonDocument(MongoCollectionImpl.java:507)
at com.mongodb.MongoCollectionImpl.insertMany(MongoCollectionImpl.java:292)
at com.mongodb.MongoCollectionImpl.insertMany(MongoCollectionImpl.java:282)
at com.foo.bar.main(bar.java:27)
Sounds like I need to work with some Mongo Codecs stuff, but I'm not familiar with it and some quick googling returns some results that seem pretty advanced.
How do I properly write my User class for use in Mongo? Here is my class for reference:
public class User extends Document {
User(String user, List<Document > history, boolean isActive, String location){
this.append("_id", user)
.append("history", history)
.append("isActive", isActive)
.append("location", location);
}
public List<Document > getHistory(){
return this.get("history", ArrayList.class);
}
public void addToHistory(Document event){
List<Document> history = this.getHistory();
history.add(event);
this.append("history", history);
}
public boolean hasMet(User otherUser){
List<String> usersIveMet = this.getUsersMet(),
usersTheyMet = otherUser.getUsersMet();
return !Collections.disjoint(usersIveMet, usersTheyMet);
}
public List<String> getUsersMet() {
List<Document> usersHistory = this.getHistory();
List<String> usersMet = usersHistory.stream()
.map(doc -> Arrays.asList(doc.getString("user1"), doc.getString("user1")))
.filter(u -> !u.equals(this.getUser()))
.flatMap(u -> u.stream())
.collect(Collectors.toList());
return usersMet;
}
public String getUser(){
return this.getString("_id");
}
}
Since you are trying to create new object (even if you extend from Document), Mongo has no way to recognize it and therefore you need to provide encoding/decoding in order to let Mongo to know about your object (at least I cannot see other way than this..).
I played with your User class a bit and get it work.
So, here is how I defined a User class:
public class User {
private List<Document> history;
private String id;
private Boolean isActive;
private String location;
// Getters and setters. Omitted for brevity..
}
Then you need provide encoding/decoding logic to your User class:
public class UserCodec implements Codec<User> {
private CodecRegistry codecRegistry;
public UserCodec(CodecRegistry codecRegistry) {
this.codecRegistry = codecRegistry;
}
#Override
public User decode(BsonReader reader, DecoderContext decoderContext) {
reader.readStartDocument();
String id = reader.readString("id");
Boolean isActive = reader.readBoolean("isActive");
String location = reader.readString("location");
Codec<Document> historyCodec = codecRegistry.get(Document.class);
List<Document> history = new ArrayList<>();
reader.readStartArray();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
history.add(historyCodec.decode(reader, decoderContext));
}
reader.readEndArray();
reader.readEndDocument();
User user = new User();
user.setId(id);
user.setIsActive(isActive);
user.setLocation(location);
user.setHistory(history);
return user;
}
#Override
public void encode(BsonWriter writer, User user, EncoderContext encoderContext) {
writer.writeStartDocument();
writer.writeName("id");
writer.writeString(user.getId());
writer.writeName("isActive");
writer.writeBoolean(user.getIsActive());
writer.writeName("location");
writer.writeString(user.getLocation());
writer.writeStartArray("history");
for (Document document : user.getHistory()) {
Codec<Document> documentCodec = codecRegistry.get(Document.class);
encoderContext.encodeWithChildContext(documentCodec, writer, document);
}
writer.writeEndArray();
writer.writeEndDocument();
}
#Override
public Class<User> getEncoderClass() {
return User.class;
}
}
Then you need a codec provided for type checking before starting serialization/deserialization.
public class UserCodecProvider implements CodecProvider {
#Override
#SuppressWarnings("unchecked")
public <T> Codec<T> get(Class<T> clazz, CodecRegistry registry) {
if (clazz == User.class) {
return (Codec<T>) new UserCodec(registry);
}
return null;
}
}
And finally, you need to register your provider to your MongoClient, that's all.
public class MongoDb {
private MongoDatabase db;
public MongoDb() {
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(
CodecRegistries.fromProviders(new UserCodecProvider()),
MongoClient.getDefaultCodecRegistry());
MongoClientOptions options = MongoClientOptions.builder()
.codecRegistry(codecRegistry).build();
MongoClient mongoClient = new MongoClient(new ServerAddress(), options);
db = mongoClient.getDatabase("test");
}
public void addUser(User user) {
MongoCollection<User> collection = db.getCollection("user").withDocumentClass(User.class);
collection.insertOne(user);
}
public static void main(String[] args) {
MongoDb mongoDb = new MongoDb();
Document history1 = new Document();
history1.append("field1", "value1");
history1.append("field2", "value2");
history1.append("field3", "value3");
List<Document> history = new ArrayList<>();
history.add(history1);
User user = new User();
user.setId("someId1");
user.setIsActive(true);
user.setLocation("someLocation");
user.setHistory(history);
mongoDb.addUser(user);
}
}
A bit late but just stumbled across the issue and was also somewhat disappointed by the work involved in the proposed solutions so far. Especially since it requires tons of custom code for every single Document extending class you wish to persist and might also exhibit sub-optimal performance noticeable in large data sets.
Instead I figured one might piggyback off DocumentCodec like so (Mongo 3.x):
public class MyDocumentCodec<T extends Document> implements CollectibleCodec<T> {
private DocumentCodec _documentCodec;
private Class<T> _class;
private Constructor<T> _constructor;
public MyDocumentCodec(Class<T> class_) {
try {
_documentCodec = new DocumentCodec();
_class = class_;
_constructor = class_.getConstructor(Document.class);
} catch (Exception ex) {
throw new MCException(ex);
}
}
#Override
public void encode(BsonWriter writer, T value, EncoderContext encoderContext) {
_documentCodec.encode(writer, value, encoderContext);
}
#Override
public Class<T> getEncoderClass() {
return _class;
}
#Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
try {
Document document = _documentCodec.decode(reader, decoderContext);
T result = _constructor.newInstance(document);
return result;
} catch (Exception ex) {
throw new MCException(ex);
}
}
#Override
public T generateIdIfAbsentFromDocument(T document) {
if (!documentHasId(document)) {
Document doc = _documentCodec.generateIdIfAbsentFromDocument(document);
document.put("_id", doc.get("_id"));
}
return document;
}
#Override
public boolean documentHasId(T document) {
return _documentCodec.documentHasId(document);
}
#Override
public BsonValue getDocumentId(T document) {
return _documentCodec.getDocumentId(document);
}
}
This is then registered along the lines of
MyDocumentCodec<MyClass> myCodec = new MyDocumentCodec<>(MyClass.class);
CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(),
CodecRegistries.fromCodecs(myCodec));
MongoClientOptions options = MongoClientOptions.builder().codecRegistry(codecRegistry).build();
MongoClient dbClient = new MongoClient(new ServerAddress(_dbServer, _dbPort), options);
Switching to this approach along with bulking up some operations (which probably has a large effect) I just managed to run an operation that previously took several hours to 30 mins. The decode method can probably be improved but my main concern was inserts for now.
Hope this helps someone. Please let me know if you see issues with this approach.
Thanks.
Have you tried using the #Embedded and #JsonIgnoreProperties(ignoreUnknown = true) on top of your class signature?
This worked for me when I had a similar issue. I had a model (Translation) which I was storing in a HashMap member field of another model (Promo).
Once I added these annotations to the Translation class signature, the issue went away. Not sure if it'll work that way in your case but worth trying.
I have to explore more on this myself.
My web app is using GWT 2.0.2, GXT 2.1.1, Hibernate 3.5-CR1, Javassist 3.11.0 and Gilead 1.3.1 (latest from SVN).
My app was running just fine with GWT 1.7.1 + Gilead 1.2.
I want to take advantage of some of the features of GWT 2.0, and figured I'd upgrade to the latest Gilead in the process. I pointed to the new gwt.xml file in Gilead 1.3. I'm also using the new net.sf.gilead.pojo.gwt.LightEntity instead of net.sf.gilead.pojo.java5.LightEntity.
I have a few Hibernate entities/classes that extend LightEntity (i.e. Question, Form), as well as a few more entities/classes that extend the Question entity. Not sure if it matters, but I'm using InheritanceType.JOINED for the inheritance strategy in the Question entity.
For reference, here's the Question class:
#Entity
#Table(name = "Questions")
#Inheritance(strategy = InheritanceType.JOINED)
public abstract class Question extends LightEntity implements IsSerializable,
IFormItem, Comparable<Question> {
private static final long serialVersionUID = 9180458948973980161L;
public static final String FIELD_NAME_PREFIX = "field_"; //$NON-NLS-1$
private static final String REQUIRED_QUESTION = "<span style=\"color: red;\">*</span>"; //$NON-NLS-1$
public static int MIN_WIDTH = 50;
public static int DEFAULT_WIDTH = 200;
public static int MAX_WIDTH = 600;
private int id;
private Integer questionOrder;
private String questionNumber;
protected String questionText;
protected boolean required;
private String toolTip;
protected Integer fieldWidth;
#Id
#GeneratedValue
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public boolean isRequired() {
return this.required;
}
public void setRequired(boolean required) {
this.required = required;
}
public String getToolTip() {
return this.toolTip;
}
public void setToolTip(String toolTip) {
this.toolTip = toolTip;
}
#Column(length = 5000)
#Lob
public String getQuestionText() {
return this.questionText;
}
public void setQuestionText(String pText) {
this.questionText = pText;
}
public Integer getQuestionOrder() {
return this.questionOrder;
}
public void setQuestionOrder(Integer questionOrder) {
this.questionOrder = questionOrder;
}
public String getQuestionNumber() {
return this.questionNumber;
}
public void setQuestionNumber(String questionNumber) {
this.questionNumber = questionNumber;
}
public boolean hasQuestionNumber() {
return getQuestionNumber() != null
&& !getQuestionNumber().trim().isEmpty();
}
public Integer getFieldWidth() {
return this.fieldWidth;
}
public void setFieldWidth(Integer fieldWidth) {
this.fieldWidth = fieldWidth;
}
public Component render(FormPanel formPanel, int order, int questionSpacing) {
final Component c = generateWidget(getId());
if (c instanceof Field<?>) {
final Field<?> field = (Field<?>) c;
field.setLabelSeparator(FormBuilderConstants.EMPTY_TEXT);
field.setValidateOnBlur(true);
field.setAutoValidate(true);
field.setName(FIELD_NAME_PREFIX.concat(String.valueOf(getId())));
if (getToolTip() != null) {
field.setToolTip(getToolTip());
}
final FormData formData;
if (field instanceof SimpleComboBox<?>) {
formData = new FormData();
} else {
if (getFieldWidth() == null) {
field.setAutoWidth(true);
formData = new FormData(FormBuilderConstants.FORM_ANCHOR_SPEC);
} else {
field.setWidth(getFieldWidth().intValue());
field.setAutoWidth(false);
formData = new FormData(getFieldWidth().intValue(), -1);
}
}
final String questionNumber;
if (this.questionNumber != null && !this.questionNumber.isEmpty()) {
questionNumber = this.questionNumber;
} else {
questionNumber = String.valueOf(order);
}
if (this.answerable()) {
String displayQuestionText = questionNumber.concat(". ") //$NON-NLS-1$
.concat(getQuestionText());
if (isRequired()) {
displayQuestionText = displayQuestionText
.concat(REQUIRED_QUESTION);
}
field.setFieldLabel(displayQuestionText);
}
field.setIntStyleAttribute("margin-bottom", questionSpacing); //$NON-NLS-1$
formPanel.add(field, formData);
} else {
formPanel.add(c);
}
return c;
}
protected abstract Component generateWidget(final int id);
public abstract String questionType();
public int compareTo(final Question q) {
return this.questionOrder.intValue() - q.questionOrder.intValue();
}
public boolean answerable() {
return true;
}
}
My app has a startup servlet that creates a Hibernate session factory. In the logs, I get a "duplicate method" error on all of the classes that directly or indirectly extend LightEntity. I wonder if this is an issue with Javassist's handling of inheritance.
16:32:59,616 DEBUG AbstractEntityPersister:2773 - Identity insert: insert into Questions (fieldWidth, questionNumber, questionOrder, questionText, required, toolTip) values (?, ?, ?, ?, ?, ?)
16:32:59,619 ERROR BasicLazyInitializer:165 - Javassist Enhancement failed: com.function1.formbuilder.client.model.Question
java.lang.RuntimeException: duplicate method: getProxyInformation in com.function1.formbuilder.client.model.Question_$$_javassist_5
at javassist.util.proxy.ProxyFactory.createClass3(ProxyFactory.java:344)
at javassist.util.proxy.ProxyFactory.createClass2(ProxyFactory.java:314)
at javassist.util.proxy.ProxyFactory.createClass(ProxyFactory.java:273)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxyFactory(JavassistLazyInitializer.java:162)
at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.postInstantiate(JavassistProxyFactory.java:66)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:188)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:151)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:78)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:107)
at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:135)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:323)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:456)
at org.hibernate.persister.entity.JoinedSubclassEntityPersister.<init>(JoinedSubclassEntityPersister.java:113)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:87)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:267)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at com.function1.common.F1HibernateUtil.<clinit>(F1HibernateUtil.java:22)
at com.function1.formbuilder.server.StartupServlet.init(StartupServlet.java:26)
Caused by: java.lang.RuntimeException: duplicate method: getProxyInformation in com.function1.formbuilder.client.model.Question_$$_javassist_0
at javassist.util.proxy.ProxyFactory.createClass3(ProxyFactory.java:344)
at javassist.util.proxy.ProxyFactory.createClass2(ProxyFactory.java:314)
at javassist.util.proxy.ProxyFactory.createClass(ProxyFactory.java:273)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxyFactory(JavassistLazyInitializer.java:162)
... 42 more
Caused by: javassist.bytecode.DuplicateMemberException: duplicate method: getProxyInformation in com.function1.formbuilder.client.model.Question_$$_javassist_0
at javassist.bytecode.ClassFile.testExistingMethod(ClassFile.java:593)
at javassist.bytecode.ClassFile.addMethod(ClassFile.java:577)
at javassist.util.proxy.ProxyFactory.override(ProxyFactory.java:658)
at javassist.util.proxy.ProxyFactory.overrideMethods(ProxyFactory.java:632)
at javassist.util.proxy.ProxyFactory.make(ProxyFactory.java:552)
at javassist.util.proxy.ProxyFactory.createClass3(ProxyFactory.java:335)
Any ideas on how to resolve this issue?
As illustrated by ticket HIBERNATE-37, somehow getProxyInformation() gets define twice, possibly with a different return type.
ticket HHH-1938 suggested using cglib instead of JavaAssist as a bytecode enhancer, but I an not sure if this is possible in your configuration.
You must change the value of
hibernate.bytecode.provider=javassist
for
hibernate.bytecode.provider=cglib
in:
<WHERE IS YOUR JBOSS>\server\default\deploy\ejb3.deployer\META-INF\ persistence.properties
And that fixes the problem of duplicated method
(again, this is not your configuration but that could give you an idea where to look)
The new javassist versoin 3.16.1-GA will work with duplicate methods:
https://issues.jboss.org/browse/JASSIST-127
And there were some other similar issues also fixed for 3.16.0-GA
Javassist doesn't allow duplicate methods (allowed by Java5)
https://jira.jboss.org/jira/browse/JASSIST-24
Try removing Comparable<Question> if that is possible.