[EJB 3.0] - How to access a join table column?!
by fabiorusso
Hello. I hope this is not a dumb question. I have two Entities with a ManyToMany bidirectional relationship. Up to this point everything is ok. But there is a column in the join table I would like to access and I really don't know how :-(
Here are my Entities: the Partner, the OptionRate and the join table. There is a column named 'isDefault' in the join table that I need to check.
Here is my code:
This is the Partner entity:
| @Entity
| @Table(name="VOW_PARTNERS")
| public class AbbeyphonePartner implements Serializable
| {
| private String partnerId;
| private String passKey;
| private String partnerDescription;
| private List <RateOption> rateOptions;
|
| @Id
| @Column(name="PARTNER_ID")
| public String getPartnerId()
| {
| return this.partnerId;
| }
|
| public void setPartnerId(String partnerId)
| {
| this.partnerId = partnerId;
| }
|
| @Column(name="PARTNER_DESCRIPTION")
| public String getPartnerDescription()
| {
| return this.partnerDescription;
| }
|
| public void setPartnerDescription(String partnerDescription)
| {
| this.partnerDescription = partnerDescription;
| }
|
| @Column(name="PASS_KEY")
| public String getPassKey()
| {
| return this.passKey;
| }
|
| public void setPassKey(String passKey)
| {
| this.passKey = passKey;
| }
|
| @ManyToMany(fetch = FetchType.EAGER , mappedBy ="partners")
| public List<RateOption> getRateOptions()
| {
| return rateOptions;
| }
|
| public void setRateOptions (List <RateOption> options)
| {
| this.rateOptions = options;
| }
| }
|
and the RateOption Entity:
| @Entity
| @Table(name="BILLING_RATEOPTIONS")
| public class RateOption implements java.io.Serializable
| {
| private Integer rateoptionId;
| private Integer calculationalgorithmId;
| private Integer priceprofileId;
| private String description;
| //private char isdefault;
| private List <AbbeyphonePartner> partners;
|
| @Id
| @Column (name="RATEOPTION_ID")
| public Integer getRateoptionId()
| {
| return this.rateoptionId;
| }
|
| public void setRateoptionId(Integer rateoptionId)
| {
| this.rateoptionId = rateoptionId;
| }
|
| @Column (name="CALCULATIONALGORITHM_ID")
| public Integer getCalculationAlgorithmId()
| {
| return this.calculationalgorithmId;
| }
|
| public void setCalculationAlgorithmId(Integer calculationalgorithmId)
| {
| this.calculationalgorithmId = calculationalgorithmId;
| }
|
| @Column (name="DESCRIPTION")
| public String getDescription()
| {
| return this.description;
| }
|
| public void setDescription(String description)
| {
| this.description = description;
| }
|
| @Column (name="PRICEPROFILE_ID")
| public Integer getPriceprofileId()
| {
| return this.priceprofileId;
| }
|
| public void setPriceprofileId(Integer priceprofileId)
| {
| this.priceprofileId = priceprofileId;
| }
|
| // @Column(name="ISDEFAULT")
| // public char getIsdefault()
| // {
| // return this.isdefault;
| // }
| //
| // /**
| // * @param isdefault the isdefault to set
| // */
| // public void setIsdefault(char isdefault)
| // {
| // this.isdefault = isdefault;
| // }
|
| @ManyToMany(fetch = FetchType.EAGER)
| @JoinTable (name="VOW_PARTNER_RATEOPTIONS", //TODO: HERE!
| joinColumns={@JoinColumn (name="RATEOPTION_ID")},
| inverseJoinColumns={@JoinColumn (name="PARTNER_ID")})
| public List<AbbeyphonePartner> getPartners()
| {
| return this.partners;
| }
|
| public void setPartners(List<AbbeyphonePartner> partners)
| {
| this.partners = partners;
| }
| }
|
The join table would be like this:
- RATEOPTION_ID Integer not null
- PARTNER_ID Integer not null
- ISDEFAULT char(1) // can be Y or N.
I need that ISDEFAULT field but I really don't know how to do it. Thank you very much for any help you can supply.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3960470#3960470
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3960470
19 years, 9 months
[JCA/JBoss] - Re: Need clarification: Trying to return an unknown connecti
by mamgoncalves
Hi,
Hibernate 3 do not use the ?Thread Local? semantic, but uses a transaction listener to close the connection. I have created some code to behave like Hibernate.
The code has the follow semantic:
1) The client call a EJB method ?doThing? that have required transaction
2) The method ?doThing? gets a connection to the database and register a transaction listener that will close the connection
3) The method ?doThing? call another EJB method ?doOtherThinkOnDiferentTransacton? that have requires new transaction
4) The method ?doOtherThinkOnDiferentTransacton? gets a new connection to the database and register a transaction listener that will close the connection
When the method ?doOtherThinkOnDiferentTransacton? is over, the container invokes the transactions listeners and then the exception ?Trying to return an unknown connection2? is written to the log. If the step 2) is not done, the exception does not appear in the log.
Here is the code:
public class Connection2BugBean implements SessionBean {
public Connection2BugBean() {
// Empty
}
public void ejbCreate() throws CreateException {
// Empty
}
public void setSessionContext(SessionContext sessionContext) throws EJBException {
// Empty
}
public void ejbRemove() throws EJBException {
// Empty
}
public void ejbActivate() throws EJBException {
// Empty
}
public void ejbPassivate() throws EJBException {
// Empty
}
public void doThing() {
MyConnectionManager.getConnection();
try {
Context context = new InitialContext();
Connection2BugHome home = (Connection2BugHome) PortableRemoteObject.narrow(context.lookup("ejb/Connection2Bug"), Connection2BugHome.class);
Connection2Bug object = home.create();
object.doOtherThinkOnDiferentTransacton();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
public void doOtherThinkOnDiferentTransacton() {
MyConnectionManager.getConnection();
}
}
class MyConnectionManager implements Synchronization {
private static final Hashtable CONNECTIONS_BY_TRANSACTIONS = new Hashtable();
private static final Logger log = Logger.getLogger(Connection2BugBean.class);
private Transaction t;
public MyConnectionManager(Transaction t) {
this.t = t;
}
public static Connection getConnection() {
try {
Context context = new InitialContext();
TransactionManager tm = (TransactionManager) context.lookup("java:/TransactionManager");
Transaction t = tm.getTransaction();
Connection conn = (Connection) CONNECTIONS_BY_TRANSACTIONS.get(t);
if (conn == null) {
log.warn("Creating a connection binded to " + t);
DataSource ds = (DataSource) context.lookup("java:/DefaultDS");
conn = ds.getConnection();
CONNECTIONS_BY_TRANSACTIONS.put(t, conn);
t.registerSynchronization(new MyConnectionManager(t));
}
return conn;
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
public void beforeCompletion() {
Connection conn = (Connection) CONNECTIONS_BY_TRANSACTIONS.remove(t);
if (conn != null) {
log.warn("Closing the connection binded to " + t);
try {
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public void afterCompletion(int i) {
}
}
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3960469#3960469
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3960469
19 years, 9 months
[JBoss Seam] - How to map DSN name with actual database ? Please help.
by laxmantr
I am trying to connect to database using hibernate in a seam project. I have put the following code in hibernate.cfg.xml
com.mysql.jdbc.Driver
jdbc:mysql://localhost/testdb
root
10
true
org.hibernate.dialect.MySQLDialect
update
<!-- Mapping files -->
I have a test program which uses the DSN to insert a record. Code goes like this:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38(a)yahoo.com");
session.save(contact);
when I run this program , database gets connected but it throws a strange exception ( ehCache exception ) in the first line of test program.
I tried various possible options. But no luck. I have been trying this from past 1 week. Please help me. Please let me know if anyone needs more information.
regards,
Laxman
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3960468#3960468
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3960468
19 years, 9 months
[JBoss Seam] - Re: Entities with one/many_to_many relationships and SEAM
by dbatcn
I hope this is not too off-topic, but I'm having some difficulty getting my Seam app up with @ManyToMany (though I suspect it's not really a Seam problem).
I've got two entity beans, User and Group, with a many-to-many relationship where User is the owner. I have a GroupManagerBean class which is a stateful session bean where I'd like to have a create() method that will create a new group with an initial user. I'm trying to figure out exactly how to annotate the classes and do the data update; my first few attempts haven't worked and I was hoping somebody could offer some advice about how to get this right: cascade types, idioms for object creation and update, etc.
I've tried to extract the relevant code snippets below. Pointers on how to do this or to previous discussions of this topic gratefully accepted.
Thanks.
User.java
| @Entity
| @Name("user")
| @Role(name="currentUser", scope=SESSION)
| @Table(name="USERS")
| public class User implements Serializable {
| ...
| private Set<Group> groups = new HashSet<Group>();
| @ManyToMany(cascade=CascadeType.ALL)
| @JoinTable(name="USER_GROUP",
| joinColumns={@JoinColumn(name="USER_ID")},
| inverseJoinColumns={@JoinColumn(name="GROUP_ID")})
| public Set<Group> getGroups() {
| return groups;
| }
| public void setGroups( Set<Group> groups ) {
| this.groups = groups;
| }
| }
|
Group.java
| @Entity
| @Name("group")
| @Table(name="GROUPS")
| public class Group implements Serializable {
| ...
| @ManyToMany(cascade=CascadeType.ALL)
| @JoinTable(name="USER_GROUP",
| joinColumns={@JoinColumn(name="USER_ID")},
| inverseJoinColumns={@JoinColumn(name="GROUP_ID")})
| public Set<Group> getGroups() {
| return groups;
| }
| public void setGroups( Set<Group> groups ) {
| this.groups = groups;
| }
| }
|
GroupManagerBean.java
| @Stateful
| @Name("groupManager")
| @LoggedIn
| public class GroupManagerBean implements GroupManager, Serializable {
| ...
| public void create() {
| group = new Group();
| group.setGroupname( newGroupname() );
| orgmobDatabase.persist( group );
| // User owns the bidirectional many-to-many relationship with Group
| user.getGroups().add( group );
| orgmobDatabase.merge( user );
| orgmobDatabase.refresh( group );
| // PROBLEM:
| // I thought that group.getUsers() would contain user, but it doesn't!
| //debug
| log.debug( "for user "+user+" created group: "+group);
| log.debug( "user "+user+" has "+user.getGroups().size()+" groups");
| log.debug( "group "+group+" has "+group.getUsers().size()+" users");
| for ( Group g : user.getGroups() ) {
| log.debug( "a group for user "+user+": "+g);
| }
| for ( User u : group.getUsers() ) {
| log.debug( "a user for group"+group+": "+u);
| }
| }
| }
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3960467#3960467
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3960467
19 years, 9 months