Pouzivam toto z konference EJB3 na jboss.org:
dsouza,

The bug has been resolved (not yet solved), so i would expect that a sollution is provided within the next week.

I've done the following work arround so that i can still use Enums:

I've added a constructor to te enum wich can contain a int value :


  | public enum Scope implements Serializable {
  | 
  | 	PUBLIC(0),
  | 	PROTECTED(1),
  | 	PRIVATE(2);
  | 	
  | 	private int type = 0;
  | 	
  | 	private Scope(int type) {
  | 		this.type = type;
  | 	}
  | 	
  | 	public int getType() {
  | 		return type;
  | 	}
  | 	
  | }
  | 

And i made the following changes to the entity:

I've made the get/set method with the enum @Transient so that it is not stored in the database. Then i created a new set of get/set methods:


  | public class MyEntity implements java.io.Serializable {
  | 
  |   private Scope scope = Scope.PUBLIC;
  |  
  |   ...
  |  
  |   public void setScopeType(int scopeType) {
  |     for (Scope scope : Scope.values()) {
  |       if (scope.getType() == scopeType) {
  |         this.scope = scope;
  |         return;
  |       }
  |     }
  | 		
  |     throw new IllegalArgumentException("ScopeType is not valid.");
  |   }
  | 	
  |   @Column(name="scope", nullable=false)
  |   public int getScopeType() {
  |     return scope.getType();
  |   }
  | }
  | 

So in my code i still use the enums but for storage i use the type value of the enum. If this bug is solved i only have to remove the added get/set method and remove the @Transient annotation.



View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3884826#3884826

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3884826


Ondřej Fafejta KYBERIE napsal(a):
Zdravím konferenci!

Mám tento výčtový typ:

public enum FolderEnum {
    INBOX, SENT, TRASH, SPAM
}

Dále mám entitu EmailMessage, která obsahuje položku FolderEnum.
Do databáze se výčtový typ mapuje jako int.
Pokud načtu celý email, tak automaticky se mi převede int na FolderEnum.

Vše fungovalo výborně až do té doby, kdy jsem potřeboval vyhledat
všechny SPAMY.

Query query = em.createQuery("SELECT em FROM EmailMessage em WHERE
em.folder=:folder");
query.setParameter("folder",FolderEnum.SPAM);
List<EmailMessage> emails=query.getResultList();

Při metodě setParametr se již nepřevádí FolderEnum.SPAM na int, ale na
BYTEA.
Vyhazuje mi to chybu:
java.sql.SQLException: ERROR: operator does not exist: integer = bytea

Jediné co funguje je toto:
query.setParameter("folder",3);

Musel bych tedy převádět FolderEnum.SPAM na int ručně, což se mi vůbec
nelíbí. (resp. udělat převodní tabulku).
Jiná možnost je místo enum používat konstanty, ale to se mi také nechce
- myslím si, že enum je elegantnější.
Zavádět číselník není potřeba, protože více položek nikdy nebude.

Setkal jste se s tím někdo?
A pokud ano, jak jste to vyřešili?

Díky
Fafi
  

Odpovedet emailem