序列化一对多的类

时间:2014-05-02 17:38:04

标签: java serialization ejb message-driven-bean

我在消息驱动的bean中使用ObjectMessage对象需要类进行序列化,现在我有两个具有onetomany关系的类,如何使它们可序列化。 javax.jms.JMSException:不序列化类

时出现序列化对象失败

出价等级

   @Entity
   @Table(name="bid")
    public class Bid implements Serializable {

private static final long serialVersionUID = 1L;
/**
 * 
 */
/**
 * 
 */

@Id
@GenericGenerator(name="autoGen" ,strategy="increment")
@GeneratedValue(generator="autoGen")
@Column(name="bidId")
private long bidId;
private float price;
@JoinColumn(name = "userId")
@ManyToOne
User user;
@JoinColumn(name = "auctionId")
@ManyToOne
Auction auction;



public Bid() {
    super();
}
public Bid(long bidId, float price, User user, Auction auction) {
    super();
    this.bidId = bidId;
    this.price = price;
    this.user = user;
    this.auction = auction;
}
public long getBidId() {
    return bidId;
}
public void setBidId(long bidId) {
    this.bidId = bidId;
}


public float getPrice() {
    return price;
}
public void setPrice(float price) {
    this.price = price;
}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}
public Auction getAuction() {
    return auction;
}
public void setAuction(Auction auction) {
    this.auction = auction;
}
}

拍卖类

     @Entity
    @Table(name="auction")
    public class Auction implements Serializable{
private static final long serialVersionUID = 1L;
/**
 * 
 */

@Id
@GenericGenerator(name="autoGen" ,strategy="increment")
@GeneratedValue(generator="autoGen")
@Column(name="auctionId")
private long auctionId;
@Column(name="itemName")
private String itemName;
@Column(name="description")
private String description;
@Column(name="openingPrice")
private float openingPrice;
@Column(name="lowestPrice")
private float lowestPrice;
@Column(name="increamentValue")
private float increamentValue;
@Column(name="startDate")
private Date  startDate;
@Column(name="endDate")
private Date endDate;
@JoinColumn(name = "userId")
@ManyToOne(cascade = CascadeType.ALL)
User user;
@JoinColumn(name = "categoryId")
@ManyToOne(cascade = CascadeType.ALL)
Category category;
@OneToMany(mappedBy="auction")
private List<Bid> bidList=new ArrayList<Bid>();
public long getAuctionId() {
    return auctionId;
}
public void setAuctionId(long auctionId) {
    this.auctionId = auctionId;
}
public String getItemName() {
    return itemName;
}
public void setItemName(String itemName) {
    this.itemName = itemName;
}

public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public float getOpeningPrice() {
    return openingPrice;
}
public void setOpeningPrice(float openingPrice) {
    this.openingPrice = openingPrice;
}
public float getLowestPrice() {
    return lowestPrice;
}
public void setLowestPrice(float lowestPrice) {
    this.lowestPrice = lowestPrice;
}
public float getIncreamentValue() {
    return increamentValue;
}
public void setIncreamentValue(float increamentValue) {
    this.increamentValue = increamentValue;
}
public Date getStartDate() {
    return startDate;
}
public void setStartDate(Date startDate) {
    this.startDate = startDate;
}
public Date getEndDate() {
    return endDate;
}
public void setEndDate(Date endDate) {
    this.endDate = endDate;
}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}
public List<Bid> getBidList() {
    return bidList;
}
public void setBidList(List<Bid> bidList) {
    this.bidList = bidList;
}
public Category getCategory() {
    return category;
}
public void setCategory(Category category) {
    this.category = category;
}
}

Messagedrivenbean class

  @MessageDriven(
    activationConfig = { @ActivationConfigProperty(
            propertyName = "destinationType", 
    propertyValue =  "javax.jms.Queue"),
            @ActivationConfigProperty(
        propertyName = "destination", propertyValue = "queue/MyQueue")
    })
   public class InsertBidMDB implements MessageListener {

BidDao bidDao=new BidDaoImpl();
/**
 * Default constructor. 
 */
public InsertBidMDB() {
    // TODO Auto-generated constructor stub
}

/**
 * @see MessageListener#onMessage(Message)
 */
public void onMessage(Message message) {

 try {
        if (message instanceof TextMessage) {
            System.out.println("Queue: I received a TextMessage at "
                            + new Date());
            TextMessage msg = (TextMessage) message;
            System.out.println("Message is : " + msg.getText());
        } else if (message instanceof ObjectMessage) {
            System.out.println("Queue: I received an ObjectMessage at "
                            + new Date());
            ObjectMessage msg = (ObjectMessage) message;
            Bid bid= (Bid) msg.getObject();
            bidDao.save(bid);

        } else {
            System.out.println("Not a valid message for this Queue MDB");
        }

    } catch (JMSException e) {
        e.printStackTrace();
    }
    // TODO Auto-generated method stub
    }

Servlet方法

       protected void doPost(HttpServletRequest request, 
       HttpServletResponse response)  throws ServletException, IOException {
    final String QUEUE_LOOKUP = "queue/MyQueue";
    final String CONNECTION_FACTORY = "ConnectionFactory";

    PrintWriter out = response.getWriter();
    try{
        Context context = new InitialContext();
        QueueConnectionFactory factory = 
            (QueueConnectionFactory)context.lookup(CONNECTION_FACTORY);
        QueueConnection connection = factory.createQueueConnection();
        QueueSession session = 
            connection.createQueueSession(false, 
                QueueSession.AUTO_ACKNOWLEDGE);

        Queue queue = (Queue)context.lookup(QUEUE_LOOKUP);
        QueueSender sender = session.createSender(queue);

        //. Sending ObjectMessage to the Queue
        ObjectMessage objMsg = session.createObjectMessage();
        User user=(User)request.getSession(true).getAttribute("user");
        long auctionId=Long.parseLong(request.getParameter("auctionId"));
        Auction auction=auctionDao.getOneAuctionById(auctionId);
        float price=Float.parseFloat(request.getParameter("price"));
        System.out.println(auctionId+""+price+""+user);
        Bid bid=new Bid();
        //bid.setUser(user);
    //  bid.setAuction(auction);
        bid.setPrice(price);
        objMsg.setObject(bid);                     
        sender.send(objMsg);
        out.println("2. Sent ObjectMessage to the Queue");

        session.close();
    }

0 个答案:

没有答案